Radio Button in Android: Learn Basics and Customization

Here is a simple tutorial to understand Radio Button in Android. I have also included mostly used customization for Android.

Radio button is a type of View that allows user to select one option from a set. Normally the available options are the ones user must see to decide.

Here is a sample code for RadioButton, place it in your .xml layout file.

    android:text="Your Text Here" />Code language: HTML, XML (xml)

Before we move any further let us first understand the RadioGroup class in Android.

And later we will also look at how we can customize RadioButton. Customization will include changing the color of RadioButton.

Understanding Radio Group for Radio Buttons

RadioGroup in Android is a class that acts as a wrapper for RadioButton. If you are using radio buttons then you have more one value to deal with.

And if you use multiple radio button without RadioGroup. You will have a bunch of radio buttons that will allow multiple selections.

But we do not want that. We want our radio button to be checked one at a time. For this purpose we will use a RadioGroup.

Sample code for radio group is given below.


        <!-- your radio buttons will go here -->

</RadioGroup>Code language: HTML, XML (xml)

Now the above code might make no sense to you. Let us use it on a real layout.

Also, note the android:orientation="horizontal" attribute I have for RadioGroup. We can use it because the class RadioGroup extends LinearLayout.

Android View Introduction: What is Android View, Using Views


Using RadioButton with RadioGroup

Here is a sample code for you we will continue our further code based on this.

First let us create a simple layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""


            android:text="Option 1" />

            android:text="Option 2" />


</LinearLayout>Code language: HTML, XML (xml)

Now let us map everything in our activity like below.

class RadioButtonActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val radioGroup: RadioGroup = findViewById(

        val optionOne: RadioButton = findViewById(
        val optionTwo: RadioButton = findViewById(



We will add interaction for the radio buttons below.

Java List Data Structure: Learn Real Use Case Of List


Handling RadioButton events

Now that we have multiple radio buttons in our Android application. Let us finally interact with them in this section.

We will breakup this section into two parts.

RadioButton click event

We obviously want to do something when a radio button is clicked. We can do exactly this by adding the code below in our activity like below.

// check which button was pressed
radioGroup.setOnCheckedChangeListener { _, checkedId ->
    when (checkedId) { -> {
            // option one was clicked
        } -> {
            // option two was clicked
}Code language: Kotlin (kotlin)

Now let us look at how we can programatically click on any of the radio button.

Clicking on RadioButton through code

It’s really simple to click any of the radio button through code. All you have to dois know the id of the button.

For this example let us click on option_two. Which can be done like below.

// select option two
radioGroup.check( language: Kotlin (kotlin)

Now that we have looked into handling click events. Let us look at customizing the radio button.

ShapableImageView in Android: Make Circular Image Border


Styling radio button in Android

Let us look at how we can customize the radio button.

Change radio button color

Let us see how we can change the color of our radio button. And by colors of radio button, I mean the text color and the button color itself.

Let us see how we can change the text color. We use the attribute named android:textColor for this.

Also, to change the radio button’s color we use android:buttonTint. If you’re on AndroidX this should work for all API levels.

Now let us extract the code to our styles.xml or themes.xml file.

<style name="CustomizedRadioButtonStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">

    <!-- to change the radio checkbox color -->
    <item name="android:buttonTint">@color/black</item>

    <!-- to change the radio checkbox color -->
    <item name="android:textColor">@color/black</item>

</style>Code language: HTML, XML (xml)

And use it in your radio buttons as

style="@style/CustomizedRadioButtonStyle"Code language: HTML, XML (xml)

The above style is going to change the color to black. Let us look at more customization.

Change radio button background

We can also change background like in other views. Let us look at this sample drawable file.

<!-- file name: rb_stateful.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">

    <!-- the active color when the radio button is being pressed -->
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <stroke android:width="2dp" android:color="#333333" />
            <solid android:color="#ff0000" />

    <!-- the color when the radio button is selected -->
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <stroke android:width="2dp" android:color="@color/purple_700" />
            <solid android:color="#ffffff" />

    <!-- the color when the radio button is not selected -->
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <stroke android:width="2dp" android:color="@android:color/darker_gray" />
            <solid android:color="@color/white" />

</selector>Code language: HTML, XML (xml)

You have to use it like any other drawable file in your andorid:background attribute.

That is all you have to know as a beginner. If you have any queries feel free to comment below.

Keep learning.


Android Get Screen Width & Density in Pixels

Using Android Handler Thread in Android for Multi-threading

Android Handler: Learn Everything About Handler

Push Notifications in Android Using One Signal

Show or Hide Soft Keyboard in Android Application And More

Related Posts