Android RadioGroup और RadioButton का पूरा गाइड
RadioGroup एक कंटेनर है जो RadioButton्स को ग्रुप करता है - एक समय में केवल एक रेडियो बटन चुना जा सकता है। सिंगल-चॉइस विकल्पों के लिए उपयोगी।
चरण 1: प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2: लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="अपना पसंदीदा विषय चुनें"
android:textSize="20sp"
android:padding="10dp" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<RadioButton
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java" />
<RadioButton
android:id="@+id/cpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Programming" />
</RadioGroup>
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="OK" />
</LinearLayout>
चरण 3: MainActivity.java - चयन पढ़ना
package com.example.andy.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnOk = findViewById(R.id.btnOk);
final RadioGroup radioGroup = findViewById(R.id.radioGroup);
btnOk.setOnClickListener(v -> {
int checkedId = radioGroup.getCheckedRadioButtonId();
if (checkedId == -1) {
// कोई चयन नहीं
btnOk.setError("कृपया एक विकल्प चुनें");
} else {
btnOk.setError(null);
RadioButton selected = findViewById(checkedId);
String subject = selected.getText().toString();
Toast.makeText(this, "चुना गया: " + subject, Toast.LENGTH_LONG).show();
}
});
}
}
RadioGroup के उपयोगी मेथड्स:
| मेथड | विवरण |
|---|---|
getCheckedRadioButtonId() | चुने गए RadioButton की ID रिटर्न करता है, -1 अगर कोई नहीं |
check(id) | प्रोग्रामेटिकली RadioButton चुनें |
clearCheck() | सभी चयन हटाएं |
setOnCheckedChangeListener() | चयन बदलने पर लिसनर |
बेहतर पैटर्न: OnCheckedChangeListener
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
if (checkedId != -1) {
RadioButton rb = findViewById(checkedId);
// तुरंत प्रतिक्रिया दें
}
});
आउटपुट



नतीजा: उपयोगकर्ता एक विषय चुनता है, OK दबाता है, और चयन टोस्ट में दिखता है।