---
title: "Radio Button in android"  
description: "DescriptionBy reading this article you will get a detailed description of a radio button and their purpose for being used Applications. It has two st"  
author: "Nitish Kesarwani"  
published: 2017-12-26  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/12743/radio-button-in-android  
category: "android apps"  
tags: ["android", "android activity", "android intent", "radiobutton"]  
reading_time: 4 minutes  

---

# Radio Button in android

##### Description

By reading this article you will get a detailed description of a [radio button](https://www.mindstick.com/articles/88/radio-buttons-in-csharp-dot-net) and their purpose for being used Applications. It has two states either on/off. It is widely used in [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), [web forms](https://www.mindstick.com/interview/33689/is-mvc-replacing-asp-dot-net-web-forms) to get information from the user.

In Radio button, there are so many [options available](https://www.mindstick.com/forum/158882/what-are-the-different-types-of-storage-options-available-in-android) in the application in web forms so that user can fill the appropriate field.

This article describes about two radio buttons and one onclick button which [response](https://yourviews.mindstick.com/view/86896/biden-s-assertive-response-to-special-counsel-critique-on-memory-strength) when a user sends a [feedback](https://www.mindstick.com/articles/12731/the-importance-of-feedback-to-the-user-experience) through radio button it produces a message on the screen through Toast. onChecked method is called when an input is given by the user clicking the button.

##### Attributes

o **android:contentDescription:** [Attribute](https://www.mindstick.com/blog/61/ado-dot-net-object-model-and-attributes) defines the description of the content item

o **android: [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp):** Drawable elements are used to set in the background

o **android: [visibility](https://www.mindstick.com/blog/301300/how-to-improve-your-visibility-in-zero-click-search-results):** It is used to set the visibility of [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net)

o **android: onClick:** Onclick handles the checked or unchecked state of a radio button

Methods

o **toggle():** This method checking the status of a checked and unchecked radio button

o **onClick():** It handles the user response when user checks the box

\

###### Android_Manifest.xml

```
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.mapsample.msclient009.radiobutton">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        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>
```

Main_Activity.xml

```
<?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="wrap_content"     android:orientation="vertical"     tools:context="com.mapsample.msclient009.radiobutton.MainActivity">     <TextView         android:layout_width="match_parent"         android:layout_height="66dp"         android:gravity="center"         android:text="Radio Button Description"         android:textSize="20dp"/>     <RadioGroup         android:id="@+id/RadioGp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="vertical">         <RadioButton             android:id="@+id/rdbtn2"             android:layout_width="391dp"             android:layout_height="wrap_content"             android:layout_weight="1"             android:text="@android:string/no"             android:textColorLink="@android:color/background_dark"             tools:text="No" />         <RadioButton             android:id="@+id/rdbtn1"             android:layout_width="364dp"             android:layout_height="47dp"             android:layout_weight="1"             android:text="@android:string/yes"             tools:text="Yes" />     </RadioGroup>     <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:onClick="onChecked"         android:text="RadioButtonChecked" /> </LinearLayout>
```

###### Main_Activity.java

```
package com.mapsample.msclient009.radiobutton;

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

public class MainActivity extends AppCompatActivity {
  RadioGroup rgp ;
  RadioButton rbtn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
            rgp =(RadioGroup)findViewById ( R.id.RadioGp );
    }

    public void onChecked(View view) {
        int selectedbtn =rgp.getCheckedRadioButtonId ();
        rbtn1 =(RadioButton)findViewById ( selectedbtn );
        if(selectedbtn== -1)
        {
            Toast.makeText (MainActivity.this,"None is selected",Toast.LENGTH_LONG ).show ();
        }
        else
        {Toast.makeText ( MainActivity.this,"You have selected one Button",Toast.LENGTH_LONG ).show ();

        }
    }
}
```

##### Launch Your APP

![Radio Button in android](https://www.mindstick.com/mindstickarticle/5767343f-56c5-4a22-a48e-8462c3a48c75/images/a4434858-9e59-4029-b345-6e20a8978146.png)

---

Original Source: https://www.mindstick.com/articles/12743/radio-button-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
