How to use RadioGroup and RadioButton in Android.

The RadioButton has two states: either checked or unchecked.
Once a RadioButton is checked, it cannot be unchecked. A RadioGroup is used to group together one or more RadioButton views, thereby allowing only one RadioButton to be checked within the RadioGroup.

The RadioGroup encloses two RadioButtons. This is important because radio buttons are usually used to present multiple options to the user for selection. When a RadioButton in a RadioGroup is selected, all other RadioButtons are automatically unselected:

Notice that the RadioButtons are listed vertically, one on top of another. If you want to list them horizontally, you need to change the orientation attribute to horizontal. You would also need to ensure that the layout_width attribute of the RadioButtons are set to wrap_content:

For RadioButton, you need to use the setOnCheckedChangeListener() method on the RadioGroup to register a callback to be invoked when the checked RadioButton changes in this group:

//Simple Radiobutton Example in android

Step 1: Firstly, open your activity_main.xml file and add following code ( Open res -> layout -> activity_main.xml and add following code:)
// activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="Who is known as King of the Fruits?"
android:textSize="30sp"
android:textColor="#00aacc"/>

<RadioGroup
android:id="@+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Apple" />

<RadioButton
android:id="@+id/mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Mango" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:id="@+id/banana"
android:text="Banana"/>

</RadioGroup>

<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:textSize="25dp"
android:layout_height="wrap_content"

android:text="Display" />

</LinearLayout>

 

Step 2: Let’s  open .Java file and and add following code (Open src -> package -> MainActivity.java and add following code: )
Code//

 

package com.example.ravi.radiobutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import static com.example.ravi.radiobutton.R.id.fruits;

public class MainActivity extends AppCompatActivity {
private RadioGroup radiofruits;
private RadioButton apple, mango, banana;
private Button displaybtn;
private TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radiofruits = (RadioGroup) findViewById(R.id.fruits);
radiofruits.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// find which radio button is selected

if (checkedId == R.id.apple) {

Toast.makeText(getApplicationContext(), "No, You are wrong",

Toast.LENGTH_SHORT).show();

} else if (checkedId == R.id.mango) {

Toast.makeText(getApplicationContext(), "Yes, this is the Right Answer",

Toast.LENGTH_SHORT).show();

} else if (checkedId == R.id.banana) {

Toast.makeText(getApplicationContext(), "No, you are Wrong",

Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(getApplicationContext(), "choice: Mango",

Toast.LENGTH_SHORT).show();
}
}
});
apple= (RadioButton) findViewById(R.id.apple);

mango = (RadioButton) findViewById(R.id.mango);

banana = (RadioButton) findViewById(R.id.banana);

textView = (TextView) findViewById(R.id.text);

displaybtn = (Button)findViewById(R.id.btnDisplay);

displaybtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int selectedId = radiofruits.getCheckedRadioButtonId();

// find which radioButton is checked by id

if(selectedId == apple.getId()) {

textView.setText("Apple");

} else if(selectedId == mango.getId()) {

textView.setText("Mango");

} else if(selectedId == banana.getId()) {

textView.setText("Banana");
} else {

textView.setText("Mango");
}

}

});
}
}

 

Step 3: Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ravi.radiobutton">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

 

Step 4 : Our output will be like this :

1

2

3

I hope you like this article.

Tags: ,