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.
Page Contents
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;
}
}
});
}
}
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
