এন্ড্রয়েডে RadioGroup এবং RadioButton ব্যবহার করার ধাপাবলী
এই উদাহরণটি দেখায় কীভাবে RadioGroup এর মধ্যে একাধিক RadioButton রাখা যায় এবং বাটন ক্লিক করলে কোনটি সিলেক্টেড তা শনাক্ত করা যায়। RadioGroup নিশ্চিত করে যে একবারে শুধু একটি রেডিও বাটন সিলেক্ট করা যাবে।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল (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"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioMale"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioFemale" />
</RadioGroup>
<Button
android:layout_below="@id/radioGender"
android:id="@+id/btnDisplay"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnDisplay" />
</RelativeLayout>
ধাপ ৩: স্ট্রিং রিসোর্স (res/values/strings.xml)
<resources>
<string name="app_name">নমুনা অ্যাপ</string>
<string name="radioMale">পুরুষ</string>
<string name="radioFemale">মহিলা</string>
<string name="btnDisplay">দেখান</string>
</resources>
ধাপ ৪: মূল এক্টিভিটি লজিক (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerButton();
}
private void addListenerButton() {
radioGroup = findViewById(R.id.radioGender);
button = findViewById(R.id.btnDisplay);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(selectedId);
Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
ধাপ ৫: ম্যানিফেস্ট ফাইল (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>
অতিরিক্ত ফিচার
RadioGroup-এ setOnCheckedChangeListener ব্যবহার করে রিয়েল-টাইমে সিলেকশন পরিবর্তন শোনা যায়:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = findViewById(checkedId);
// রিয়েল-টাইম হ্যান্ডলিং
}
});
অ্যাপ্লিকেশন চালান
আপনার আসল এন্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে অনুমান করি। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আপনার ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।