RadioButton is a widget used in android which provides functionality to choose a single option from multiple sets of options. This is generally used when we want the user to select only one option from the set of given options. In this article, we will take a look at How to use Radio Buttons on Android.
Note: This Android article covers in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--on below line we are creating
a text for our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idTVStatus"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Radio Button in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!--on below line we are creating a text view-->
<TextView
android:id="@+id/idTVStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idRadioGroup"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Status"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!--on below line we are creating a radio group-->
<RadioGroup
android:id="@+id/idRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<!--on below line we are creating a radio buttons-->
<RadioButton
android:id="@+id/idBtnJavaRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Java"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
<RadioButton
android:id="@+id/idBtnKotlinRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Kotlin"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
<RadioButton
android:id="@+id/idBtnFlutterRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Flutter"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
</RadioGroup>
</RelativeLayout>
Step 3: Working with the MainActivity file
Navigate to app > java > your app's package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// on below line we are creating a variable.
lateinit var radioGrp: RadioGroup
lateinit var statusTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing our variables.
radioGrp = findViewById(R.id.idRadioGroup)
statusTV = findViewById(R.id.idTVStatus)
radioGrp.setOnCheckedChangeListener { group, checkedId ->
// Get the selected Radio Button
val radioButton = group
.findViewById(checkedId) as RadioButton
// on below line we are setting
// text for our status text view.
statusTV.text = radioButton.text
}
}
}
Java
package com.gtappdevelopers.kotlingfgproject;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// on below line we are creating a variable.
private RadioGroup radioGrp;
private TextView statusTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our variables.
radioGrp = findViewById(R.id.idRadioGroup);
statusTV = findViewById(R.id.idTVStatus);
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Get the selected Radio Button
RadioButton radioButton = group.findViewById(checkedId);
// on below line we are setting text
// for our status text view.
statusTV.setText(radioButton.getText());
}
});
}
}
Now run your application to see the output of it.
Output:
Similar Reads
ToggleButton in Android ToggleButton is used to allow users to perform two operations on a single button. These are used to perform on and off operations like that of Switch. ToggleButton can perform two different operations on clicking on it. In this article, we will take a look at How to implement ToggleButton in Android
4 min read
RadioGroup in Android RadioGroup is a widget in android which is used to handle multiple radio buttons within the android application. We can add multiple radio buttons to our RadioGroup. We have seen how to use radio buttons in android. In this article, we will take a look at How to implement Radio Group in the android
3 min read
RadioButton in Kotlin Android Radio Button is bi-state button which can either be checked or unchecked. Also, it's working is same as Checkbox except that radio button can not allow to be unchecked once it was selected. Generally, we use RadioButton controls to allow users to select one option from multiple options. By d
4 min read
Button in Android In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it. This article demonstrates how to create a button in Android Studio.Class Hierarchy of the Button Class in Kotlinkotlin.
3 min read
Dynamic RadioButton in Kotlin An Android RadioButton is a bi-state button that can be either checked or unchecked. It functions similarly to a CheckBox, but with one key difference: once selected, a RadioButton cannot be unchecked by tapping it again. RadioButtons are typically used within a RadioGroup to allow users to select o
2 min read
RadioButtons in Android using Jetpack Compose RadioButtons are used to select only a specific option from the list of several options. Radio buttons are used in many places for selecting only one option from the list of two or more options. In this article, we will take a look at the implementation of this Radio button in Android using Jetpack
3 min read
How to Switch Themes in Android Using RadioButtons? We have seen android app comes with two different types of mode or theme which are dark mode or light mode or we also called them as night and morning mode. In this article, we will implement the light and dark mode in android using RadioButton. What we are going to build in this article? We will
4 min read
Liquid Button in Android In this article we are going to implement a liquid button library, This can be used to show animation on a button. When a user completed a course or completes Filling all the details on a form successfully, then we can show this liquid button to show success in a good way. Let's see the implementati
2 min read
Implement RadioButton with Custom Layout in Android RadioButtons allow the user to select one option from a set. You should use RadioButtons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. In this article, we are going to see how we can implement RadioButton with custom layout
3 min read