Using Radio Buttons in Android
A radio button is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options. When trying to use it in android, it might be a little bit confusing if not well understood. The codes below will give you a better understanding of how to use radio buttons in android assuming that we are developing a quiz app.
Case 1: Using Listening Event
/*XML when using listening event*/
<RadioGroup
android:id="@+id/android_question_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><RadioButton
android:id="@+id/android_virtual_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/question_margin"
android:layout_marginLeft="@dimen/question_margin"
android:layout_marginRight="@dimen/question_margin"
android:layout_marginStart="@dimen/question_margin"
android:layout_marginTop="@dimen/question_margin"
android:background="@drawable/semi_rounded_corners"
android:padding="@dimen/text_padding"
android:text="@string/android11"
android:textColor="@color/colorGrey" />//other radio buttons goes here</RadioGroup>
Java Code for Case 1
Note that listeners are meant to be set on the variable holding the id of the Radio Group.
public class AndroidActivity extends AppCompatActivity {
int score = 0;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android);//find the id of the RadioGroup and store it in a variable
RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.android_question_one);
question1RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//set a listener on the RadioGroup
@Override
public void onCheckedChanged(RadioGroup view, @IdRes int checkedId) {
//checkedId refers to the selected RadioButton
//Perform an action based on the option chosen
if (checkedId == R.id.android_virtual_device) {
getScore(1);
} else {
getScore(0);
}
}
});
}// calculate the score
private void getScore(int current_score) {
score += current_score;
}
Case 2: Using onClick Event
/*XML when using onClick event*/
<RadioGroup
android:id="@+id/android_question_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><RadioButton
android:id="@+id/android_virtual_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/question_margin"
android:layout_marginLeft="@dimen/question_margin"
android:layout_marginRight="@dimen/question_margin"
android:layout_marginStart="@dimen/question_margin"
android:layout_marginTop="@dimen/question_margin"
android:background="@drawable/semi_rounded_corners"
android:onClick="question1"
android:padding="@dimen/text_padding"
android:text="@string/android11"
android:textColor="@color/colorGrey" />//other radio buttons goes here</RadioGroup>
Java Code for Case 2
public class AndroidActivity extends AppCompatActivity {
int score = 0;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android);
}/*Calculate the score for each question*/
public void question1(View view) {//find the id of the RadioGroup and store it in a variable
RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.android_question_one);//Get the id of the RadioButton that is checked and store it as an integer variable.
int answerId1 = question1RadioGroup.getCheckedRadioButtonId();//Perform an action based on the option chosen
if (answerId1 == R.id.android_virtual_device) {
getScore(1);
} else {
getScore(0);
}
}// calculate the score
private void getScore(int current_score) {
score += current_score;
}
Using Case 1 is preferable to 2 though both can be used to achieve same result.
Keep Coding!
Thank you for reading!
If you like my article, please hit this icon 👏 below as many times as possible, bookmark, share it with your friends and follow me for more stories. Feel free to ask questions. I welcome feedback.