RadioGroup থেকে সিলেক্ট করা রেডিও বাটনের ইনডেক্স এবং মান বের করা
RadioGroup একাধিক RadioButton-কে গ্রুপ করে, যাতে একবারে শুধু একটি সিলেক্ট করা যায়। এই উদাহরণটি দেখায় কীভাবে সিলেক্ট করা আইটেমের ইনডেক্স, আইডি এবং টেক্সট পাওয়া যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML (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:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Select an option"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvResult"
android:text="Which is your most favorite?"
android:textSize="16sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:orientation="vertical"
android:padding="16dp">
<RadioButton
android:id="@+id/rbNetflix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:text="Netflix" />
<RadioButton
android:id="@+id/rbAmazonPrime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:text="Amazon Prime" />
<RadioButton
android:id="@+id/rbDisneyPlus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:text="Disney+" />
<RadioButton
android:id="@+id/rbHulu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:text="Hulu" />
</RadioGroup>
<Button
android:id="@+id/btnGetItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Get Selected Radio Button" />
</RelativeLayout>
ধাপ ৩: MainActivity.java বাস্তবায়ন
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView tvResult;
Button btnGetItem;
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tvResult);
btnGetItem = findViewById(R.id.btnGetItem);
radioGroup = findViewById(R.id.radioGroup);
// অপশনাল: রেডিও গ্রুপ চেঞ্জ লিসেনার (রিয়েল-টাইম আপডেটের জন্য)
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton selectedRb = findViewById(checkedId);
if (selectedRb != null) {
tvResult.setText("Selected: " + selectedRb.getText());
}
}
});
btnGetItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSelectedRadioInfo();
}
});
}
private void getSelectedRadioInfo() {
// ১. চেক করা রেডিও বাটনের আইডি পান
int checkedId = radioGroup.getCheckedRadioButtonId();
if (checkedId == -1) {
// কোনো বাটন সিলেক্ট করা নেই
tvResult.setText("Nothing selected from the radio group");
Toast.makeText(this, "Please select an option", Toast.LENGTH_SHORT).show();
return;
}
// ২. ভিউ পান
RadioButton selectedRadioButton = findViewById(checkedId);
String selectedText = selectedRadioButton.getText().toString();
// ৩. ইনডেক্স (পজিশন) বের করুন
int index = -1;
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (radioGroup.getChildAt(i).getId() == checkedId) {
index = i;
break;
}
}
// ৪. ফলাফল দেখান
String result = String.format(
"Selected: %s\nIndex: %d\nID: %d",
selectedText, index, checkedId
);
tvResult.setText(result);
Toast.makeText(this, "Index: " + index + " - " + selectedText, Toast.LENGTH_SHORT).show();
}
// বোনাস: প্রোগ্রাম্যাটিক্যালি সিলেকশন সেট/ক্লিয়ার করা
private void setSelectionByIndex(int index) {
if (index >= 0 && index < radioGroup.getChildCount()) {
View child = radioGroup.getChildAt(index);
if (child instanceof RadioButton) {
((RadioButton) child).setChecked(true);
}
}
}
private void clearSelection() {
radioGroup.clearCheck();
}
}
RadioGroup-এর গুরুত্বপূর্ণ মেথডها
| মেথড | ব্যাখ্যা |
|---|---|
| getCheckedRadioButtonId() | সিলেক্ট করা রেডিও বাটনের আইডি রিটার্ন করে (নেইলে -1) |
| clearCheck() | সকল সিলেকশন মুছে দেয় |
| check(int id) | নির্দিষ্ট আইডিয়ের বাটন সিলেক্ট করে |
| getChildAt(int index) | ইনডেক্স অনুযায়ী চাইল্ড ভিউ পায় |
| getChildCount() | মোট চাইল্ড (RadioButton) সংখ্যা |
| setOnCheckedChangeListener() | সিলেকশন পরিবর্তন লিসেনার |
XML-এ ডিফল্ট সিলেকশন সেট করা
<RadioGroup
android:checkedButton="@+id/rbNetflix">
<RadioButton
android:id="@+id/rbNetflix"
... />
</RadioGroup>
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন টেস্ট করুন
অ্যাপ রান করুন। RadioButton-এ ক্লিক করলে রিয়েল-টাইম আপডেট হবে। "Get Selected Radio Button" বাটনে ক্লিক করলে সিলেক্ট করা অপশনের নাম, ইনডেক্স (০-ভিত্তিক) এবং আইডি দেখাবে।