Intent এবং Bundle দিয়ে প্রাইমিটিভ ডেটা পাস করা
এই উদাহরণে দেখানো হয়েছে কিভাবে EditText থেকে নাম এবং Spinner থেকে বয়স গ্রুপ নিয়ে Bundle-এ রাখে দ্বিতীয় এক্টিভিটিতে পাঠানো যায়। putString(), putInt() ইত্যাদি প্রাইমিটিভ মেথড ব্যবহার করা হয়েছে।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: প্রথম লেআউট (activity_first.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:id="@+id/activity_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp"
tools:context=".FirstActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="নাম:" />
<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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="16dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="বয়স গোষ্ঠী:" />
<Spinner
android:id="@+id/ageGroupSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75" />
</LinearLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:text="জমা দিন" />
</LinearLayout>
</RelativeLayout>
ধাপ ৩: দ্বিতীয় লেআউট (activity_second.xml) - আউটপুট
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/displayMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center" />
</LinearLayout>
</RelativeLayout>
ধাপ ৪: FirstActivity.java - ডেটা সংগ্রহ ও পাঠানো
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class FirstActivity extends AppCompatActivity {
Button btnSubmit;
EditText editTxtName;
Spinner ageGroupSpinner;
int selectedAgeGroup = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
btnSubmit = findViewById(R.id.btnSubmit);
editTxtName = findViewById(R.id.editTxtName);
ageGroupSpinner = findViewById(R.id.ageGroupSpinner);
// Spinner-এ ডেটা সেট করুন (strings.xml-এ array সংজ্ঞায়িত করুন)
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.age_groups, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageGroupSpinner.setAdapter(adapter);
ageGroupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override public void onItemSelected(AdapterView> parent, View view, int pos, long id) {
selectedAgeGroup = pos;
}
@Override public void onNothingSelected(AdapterView> parent) {}
});
btnSubmit.setOnClickListener(v -> {
String name = editTxtName.getText().toString().trim();
if (name.isEmpty()) {
Toast.makeText(this, "নাম খালি রাখা যাবে না", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putInt("ageGroup", selectedAgeGroup);
intent.putExtra("userBundle", bundle);
startActivity(intent);
});
}
}
ধাপ ৫: SecondActivity.java - ডেটা গ্রহণ ও লজিক
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView displayMsg = findViewById(R.id.displayMsg);
Bundle bundle = getIntent().getBundleExtra("userBundle");
if (bundle != null) {
String name = bundle.getString("name", "");
int ageGroup = bundle.getInt("ageGroup", 0);
String message = "হ্যালো, " + name + "! ";
switch (ageGroup) {
case 0: message += "আপনার জীবন উপভোগ করুন"; break;
case 1: message += "জীবনকে খুব সিরিয়াসলি না নিন, মজা করুন!"; break;
case 2: message += "পুরোনো স্মৃতির সাথে জীবন উদযাপন করুন!"; break;
}
displayMsg.setText(message);
}
}
}
ধাপ ৬: strings.xml-এ Spinner ডেটা যোগ করুন
<string-array name="age_groups">
<item>১৮-২৫ বছর</item>
<item>২৬-৩৫ বছর</item>
<item>৩৫+ বছর</item>
</string-array>
ফলাফল
নাম লিখে, বয়স গোষ্ঠী সিলেক্ট করে "জমা দিন" বাটনে ট্যাপ করলে দ্বিতীয় স্ক্রিনে পсонаলাইজড মেসেজ দেখাবে।