Browse By

Android: Radio Button and Radio Group Example in Android Studio 2.0

In the previous article, we have seen how to create Spinner (Drop-Down List) Example in Android Studio 2.0. In this tutorial, we will create a simple application for Radio Button and Radio Group controller in Android 2.0.

What is Radio Button and Radio Group in Android?Android: Radio Button and Radio Group Example in Android Studio 2.0

Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. As radio buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time.

In this tutorial, the application will ask a user, how was today’s tutorial? The user will have four options like (Excellent, Good, Average and Poor). The user will be able to select only one of the option.

Direct Download Full Source Code

Android: Radio Button and Radio Group Example in Android Studio 2.0

Create a new project and fill the required details File ⇒ New ⇒ New Project.

  1. After Creating Project, open res –> layout

2. Open content_main.xml. Add Radio group control and add radio button controls inside radio group.
You can also provide orientation to radio group control according to your requirement of displaying radio buttons horizontally or vertically.

<RadioGroup
        android:id="@+id/rgOpinion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        >
        <RadioButton
            android:id="@+id/radiobtnexclnt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Excellent">
        </RadioButton>
        <RadioButton
            android:id="@+id/radiobtngood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Good">
        </RadioButton>
        <RadioButton
            android:id="@+id/radiobtnavg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Average">
        </RadioButton>
        <RadioButton
            android:id="@+id/radiobtnpoor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Poor">
        </RadioButton>
    </RadioGroup>

3. Now open your Main.Activity.java and initialize the Radio group control in it.

public class MainActivity extends AppCompatActivity implements View.OnClickListener
 {
    // ui members
    RadioGroup rgOpinion;
    Button btnSubmit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        rgOpinion = (RadioGroup) findViewById(R.id.rgOpinion);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(this);

    }

4. Now handle click event of submit button using onClick() method in the main class.

  • To get selected Radio Button ID, use getCheckedRadioButtonId() method.
  • To see Radio Button is selected or not, use isChecked() method.
public void onClick(View v)
 {
    // TODO Auto-generated method stub
    if (v == btnSubmit) {
        // Get Selected Radio Button and display output
        RadioButton selectRadio = (RadioButton) findViewById(rgOpinion
                .getCheckedRadioButtonId());

        // Get Selected Radio Button text here
        String opinion = selectRadio.getText().toString(); 
        Toast.makeText(this, "Your Opinion is : " + opinion, Toast.LENGTH_LONG).show(); }
 }

5. Now Execute the application

This slideshow requires JavaScript.

I hope you like this article. Share your reviews to improve content. Please stay tune with us. In next article, we will migrate CheckBox example into Android Studio 2.0 from eclipse. Happy coding!!

Subscribe for more articles!!

Download Full Source Code