RadioButton in Android

In this tutorial, we will learn about RadioButton in Android. Radio buttons are kind of buttons which has only two states checked or unchecked the same as CheckBox expects with little differences. RadioButton in Android is generally used with another widget which in RadioGroup. Suppose a RadioGroup contain two Radio Buttons it means we can select only one RadioButton at a single time. We can not select both Radio Buttons once. For Example, we have Radio Group of two RadioButtons with Male or Female option in our signup form in this case user can only select one radio button to mention their gender. Throughout this tutorial learn how to add RadioButtons in RadioGroup widget in the XML layout file and how to get selected RadioButton in Android in particular RadioGroup using Java file.

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="Gender"
        android:textSize="18sp"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:id="@+id/gender">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/male"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/female"/>

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"/>

</LinearLayout>

Output:

We got this output after writing this code in out XML file. Here we will write java code to make things working.

Java Code:

public class MainActivity extends AppCompatActivity {

    RadioGroup gender;
    Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gender=(RadioGroup)findViewById(R.id.gender);
        submit=(Button)findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (gender.getCheckedRadioButtonId()){
                    case R.id.male:
                        Toast.makeText(getApplication(),"You Selected Male",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.female:
                        Toast.makeText(getApplication(),"You Selected Female",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

    }
}

 

Spread the love
Scroll to Top
×