RadioGroup and RadioButton


In this tutorial we want to explore RadioGroup and RadioButton, see how to work with these two.

RadioButton is a compound button and is normally used in conjuction with RadioGroup. Thus checking one RadioButton unchecks the others.

Let's see.

Video tutorial(ProgrammingWizards TV Channel)

Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel. Basically we have a TV for programming where do daily tutorials especially android.

RadioButton and RadioGroup Example

Let's look at an example.

Android RadioButtons and RadioGroup

Android RadioButtons and RadioGroup

Resoources.

Android platform provides a powerful and flexible way of adding static content as a resource.

These static content will also be packaged into the APK file. The static content will be stored either as a resource or as an asset.

Resources belong to a given type. These types can be:

  1. Drawable.
  2. Layout.
  3. Value.

Let's start by looking at the layout resources

(a). activity_main.xml

First we will create our activity_main.xml layout. At the root we have a RelativeLayout.

Inside the relativelayout we have our header label, basically a TextView widget which will show our header text.

Then we create a RadioGroup, this RadioGroup will wrap our RadioButtons.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="info.camposha.msradiobutton.MainActivity">

    <TextView
        android:id="@+id/headerLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="casual"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="RadioGroup and RadioButton"
        android:textAllCaps="true"
        android:textSize="24sp"
        android:textStyle="bold" />
    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/americaRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="America"
            android:padding="5dp" />
        <RadioButton
            android:id="@+id/europeRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Europe"
            android:padding="5dp" />
        <RadioButton
            android:id="@+id/asiaRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Asia"
            android:padding="5dp" />
        <RadioButton
            android:id="@+id/africaRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Africa"
            android:padding="5dp" />
    </RadioGroup>
</RelativeLayout>

MainActivity.java

Here's our main activity file. Its deriving from android.app.Activity.

As instance fields we will have a RadioGroup and multiple RadioButtons.

Then we will initialize them using the findViewById() method.

We will then listen to the CheckedChangeListener listener of our radiogroup.

When any RadioButton in our radioGroup is checked, we will first obtain it's id:

int checkedItemId = myRadioGroup.getCheckedRadioButtonId();

Then we can use the id to to find the actual radiobutton:

RadioButton checkedRadioButton=findViewById(checkedItemId);

Then get it's value and show in a Toast message:

Toast.makeText(MainActivity.this,checkedRadioButton.getText(),Toast.LENGTH_SHORT).show();

here's the full code:

package info.camposha.msradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

    RadioGroup myRadioGroup;
    RadioButton americaRadioButton,europeRadioButton,asiaRadioButton,africaRadioButton;

    /*
    Initialize Widgets
     */
    private void initializeWidgets()
    {
        //reference the widgets
        myRadioGroup=findViewById(R.id.myRadioGroup);
        americaRadioButton=findViewById(R.id.americaRadioButton);
        europeRadioButton=findViewById(R.id.europeRadioButton);
        asiaRadioButton=findViewById(R.id.asiaRadioButton);
        africaRadioButton=findViewById(R.id.africaRadioButton);

        //when any radiobutton in our radiogroup is checked, show its value
        myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int checkedItemId = myRadioGroup.getCheckedRadioButtonId();
                RadioButton checkedRadioButton=findViewById(checkedItemId);
                Toast.makeText(MainActivity.this,checkedRadioButton.getText(),Toast.LENGTH_SHORT).show();
            }
        });

    }
    /*
    when activity is created
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeWidgets();
    }
}

Best regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post