কম্পিউটার

অ্যান্ড্রয়েড অ্যাপ্লিকেশনে কার্যকলাপের মধ্যে ডেটা কীভাবে পাস করবেন?


এই উদাহরণটি দেখায় কিভাবে Android অ্যাপ্লিকেশনের কার্যকলাপের মধ্যে ডেটা পাস করতে হয়৷

ধাপ 1 − অ্যান্ড্রয়েড স্টুডিওতে একটি নতুন প্রকল্প তৈরি করুন, ফাইল ⇒ নতুন প্রকল্পে যান এবং একটি নতুন প্রকল্প তৈরি করতে সমস্ত প্রয়োজনীয় বিবরণ পূরণ করুন৷

ধাপ 2 − res/layout/activity_main.xml-এ নিম্নলিখিত কোড যোগ করুন।

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/activity_first"
   android:paddingTop="60dp"
   tools:context=".FirstActivity">
   <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:paddingLeft="10sp">
         <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"/>
         <EditText
            android:id="@+id/editTxtName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.75"
            android:ems="10" />
      </LinearLayout>
      <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:paddingLeft="10sp"
         android:paddingTop="16dp">
         <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25" />
         <Spinner
            android:id="@+id/ageGroupSpinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.75"/>
      </LinearLayout>
      <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:paddingTop="16dp">
         <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50" />
         <Button
            android:id="@+id/btnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"/>
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>

ধাপ 3 − res/layout/activity_second.xml

-এ নিম্নলিখিত কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/activity_second"
   android:paddingTop="60dp">
   <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:paddingLeft="10sp">
         <TextView
            android:id="@+id/displayMsg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25" />
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>

ধাপ 4 − src/MainActivity.java

-এ নিম্নলিখিত কোড যোগ করুন
package com.example.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class FirstActivity extends AppCompatActivity {
   Button btnSubmit = null;
   EditText editTxtName = null;
   private static final String STING_EMPTY = "";
   private static int ageGroup = 0;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_first);
      btnSubmit = (Button) findViewById(R.id.btnSubmit);
      btnSubmit.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            editTxtName = (EditText) findViewById(R.id.editTxtName);
            if (STING_EMPTY.equals(editTxtName.getText().toString())) {
               Toast.makeText(FirstActivity.this, "Name Cannot be empty!",
               Toast.LENGTH_LONG).show();
            } else {
               Spinner ageGroupSpinner = (Spinner)findViewById(R.id.ageGroupSpinner);
               ageGroupSpinner.setOnItemSelectedListener(
               new AdapterView.OnItemSelectedListener() {
                  @Override
                  public void onItemSelected(AdapterView<?> adapterView, View view,
                  int i, long l) {
                     //set the corresponding age group when the user changes
                     // the age group from dropdown
                     ageGroup = i;
                  }
                  @Override
                  public void onNothingSelected(AdapterView<?> adapterView) {
                  }
               });
               //Create a new intent for the SecondActivity
               Intent i = new Intent(FirstActivity.this,SecondActivity.class);
               //Populate the input values in a bundle and pass it to SecondActivity
               Bundle b = new Bundle();
               b.putString("name",editTxtName.getText().toString());
               b.putInt("ageGroup",ageGroup);
               //Set the userBundle to the intent
               i.putExtra("userBundle",b);
               startActivity(i);
            }
         }
      });
   }
}

ধাপ 5 − src/SecondActivity.java

-এ নিম্নলিখিত কোড যোগ করুন
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      //get the passed intent
      Intent i = getIntent();
      //get the bundle stored inside the intent
      Bundle b = i.getBundleExtra("userBundle");
      TextView displayMsg = (TextView) findViewById(R.id.displayMsg);
      String message = "Hi, " + b.getString("name") + " ";
      int ageGroup = b.getInt("ageGroup");
      switch (ageGroup){
         case 0: message = message + "Enjoy your life";
         break;
         case 1: message = message + "Don't take life too seriously.. Have fun!";
         break;
         case 2: message = message + "Celebrate your life with old memories!";
         break;
      }
      displayMsg.setText(message);
   }
}

ধাপ 6 - Manifests/AndroidManifest.xml

-এ নিম্নলিখিত কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="com.example.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".FirstActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity android:name=".SecondActivity"></activity>
   </application>
</manifest>

আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করা যাক. আমি ধরে নিচ্ছি আপনি আপনার কম্পিউটারের সাথে আপনার আসল অ্যান্ড্রয়েড মোবাইল ডিভাইসটি সংযুক্ত করেছেন৷ অ্যান্ড্রয়েড স্টুডিও থেকে অ্যাপটি চালাতে, আপনার প্রোজেক্টের অ্যাক্টিভিটি ফাইলগুলির একটি খুলুন এবং রান এ ক্লিক করুন অ্যান্ড্রয়েড অ্যাপ্লিকেশনে কার্যকলাপের মধ্যে ডেটা কীভাবে পাস করবেন? টুলবার থেকে আইকন। একটি বিকল্প হিসাবে আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপরে আপনার মোবাইল ডিভাইসটি পরীক্ষা করুন যা আপনার ডিফল্ট স্ক্রীন প্রদর্শন করবে –

অ্যান্ড্রয়েড অ্যাপ্লিকেশনে কার্যকলাপের মধ্যে ডেটা কীভাবে পাস করবেন?

অ্যান্ড্রয়েড অ্যাপ্লিকেশনে কার্যকলাপের মধ্যে ডেটা কীভাবে পাস করবেন?


  1. অ্যান্ড্রয়েডে বিভিন্ন ক্রিয়াকলাপের মধ্যে কীভাবে স্যুইচ করবেন?

  2. অ্যান্ড্রয়েডে মোবাইল ডেটা কীভাবে নিষ্ক্রিয় করবেন?

  3. কিভাবে প্রোগ্রামিকভাবে অ্যাপ্লিকেশন প্রস্থান করবেন?

  4. অ্যান্ড্রয়েডে ডেটা কীভাবে বাঁধবেন