In this tutorial, we are going to discuss about android radio button widget. We will also learn about different attributes of radio button that are used to customise this widget.
Android RadioButton can be defined as below –
"RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make the radio button checked. RadioButton is usually used with RadioGroup."
RadioButtons are normally used in the RadioGroup. Once a radio button is checked, it cannot be marked as user unchecked. When there are many radio buttons in Radio Group, Checking one radio button unchecks all other radio buttons.
S. No. | XML Attributes | Description |
1 | android:clickable | Set true/false depending on whether you want to make this view clickable. Set true when you want to make this view clickable. |
2 | android:drawableBottom | Sets drawable to show at bottom of the text. |
3 | android:drawableEnd | Sets drawable to show at end of the text. |
4 | android:drawableLeft | Sets drawable to show at left of the text. |
5 | android:drawablePadding | Sets padding of the drawable. |
6 | android:backgroundTint | Sets tint to the background of View. |
Some of the popular attributes of RadioButton inherited from Compound Button are –
S. No. | XML Attributes | Description |
1 | android:button | Drawable to be used for button graphic. |
2 | android:buttonTint | Sets tint to the button. |
Some of the popular attributes of RadioButton inherited from View are –
S. No. | XML Attributes | Description |
1 | android:id | This is unique id of the RadioButton to uniquely identify the RadioButton. |
2 | android:tooltipText | Define the text to be show as tooltip when cursor is hovered on it. |
3 | android:visibility | Sets whether to show/hide this view. |
4 | android:onClick | Define what to do when view is clicked. |
5 | android:background | Sets background. |
6 | android:padding | Sets padding of View. |
7 | android:alpha | Defines alpha in view. |
At first, we will create android application. Then, we will use RadioButton in android application.
Follow the steps below to create new project. Please ignore steps if you've already created a new application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Go to File => New => New Project. Write application name as RadioButton. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you've followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Now, we will modify xml and java file to use RadioButton in the application.
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">RadioButton</string>
<string name="radio_Male">Male</string>
<string name="radio_Female">Female</string>
</resources>
Other values folder have not been changed. So, we're not going to mention it here.
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_Male"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_Female"/>
</RadioGroup>
</android.support.constraint.ConstraintLayout>
As mentioned earlier, it is not possible to uncheck the RadioButton by clicking it once it is checked. We use it with RadioGroup and other RadioButtons. When several radio buttons are used in a radio group, clicking on any radio button checks it and unchecks all other radio buttons. Thus, at any time only one radio button can be selected.
We used two RadioButtons inside a RadioGroup in the activity_main.xml file. One radio button represents a male gender, another represents a female. So, if you want to select a gender, you can select either a male or a female.
Radio button is used in similar situations as if you only want to select one option out of many options.
Open src/main/java/com.ukacademe.radiobutton/MainActivity.java file. Then, add below code into it.
package com.ukacademe.radiobutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
if (radioGroup != null) {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String text = "You Selected: ";
text += (R.id.radioMale == checkedId) ? "Male" : "Female";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
}
}
}
We access RadioButtons defined in xml file in the MainActivity file. Then, when selection changes in the RadioGroup, i.e., we show a toast message when any radio button is selected, we will display a toast message.
Since the file of AndroidManifest.xml is very important in any android application. We are also going to see the content of this file.
Code inside src/main/AndroidManifest.xml file is as below &ndash
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.radiobutton">
<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>
When you run the application, you will get output as shown below.