Before getting into example, we should know, What is radio group in android. Radio Group contains group of radio buttons. Using radio buttons we can select and un select according to user requirements.
This example demonstrate about how to use radio group in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select your favourite subject" android:textSize="20sp" android:padding="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp"> <RadioButton android:id="@+id/adroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android"/> <RadioButton android:id="@+id/java" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="java"/> <RadioButton android:id="@+id/cpp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CPP"/> <RadioButton android:id="@+id/clan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C Programming"/> </RadioGroup> <Button android:id="@+id/buton" android:text="ok" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
In the above code it contained four radio buttons, When user click on the button it going to get the text of checked radio button.
Step 3 − Add the following code to src/MainActivity.java
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.util.Linkify; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { RadioButton radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button=findViewById(R.id.buton); final RadioGroup radioGroup=findViewById(R.id.radioGroup); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int clickedRadioButton=radioGroup.getCheckedRadioButtonId(); radioButton=findViewById(clickedRadioButton); if(clickedRadioButton ==-1){ button.setError("Error"); } else { button.setError(null); Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_LONG).show(); } } }); } }
Step 5 − No need to change manifest.xml Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Select any subject, as an example we have selected java as shown below -
Now Click on ok button to get the text of selected radio button from radio group.
In the above result we have selected ok button, it will show toast of selected radio button text.