RadioButton in Android

 

Android RadioButton

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.

RadioButton is generally used with RadioGroup. RadioGroup contains several radio buttons, marking one radio button as checked makes all other radio buttons as unchecked.

Example of Radio Button

In this example, we are going to implement single radio button separately as well as radio button in RadioGroup.

activity_main.xml

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="example.javatpoint.com.radiobutton.MainActivity">  
  9.   
  10.     <TextView  
  11.         android:id="@+id/textView1"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginTop="30dp"  
  15.         android:gravity="center_horizontal"  
  16.         android:textSize="22dp"  
  17.         android:text="Single Radio Buttons" />  
  18.   
  19.   
  20.   
  21.     <!--   Default RadioButtons  -->  
  22.   
  23.     <RadioButton  
  24.         android:id="@+id/radioButton1"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_gravity="center_horizontal"  
  28.         android:text="Radio Button 1"  
  29.         android:layout_marginTop="20dp"  
  30.   
  31.         android:textSize="20dp" />  
  32.     <RadioButton  
  33.         android:id="@+id/radioButton2"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="Radio Button 2"  
  37.         android:layout_marginTop="10dp"  
  38.   
  39.         android:textSize="20dp" />  
  40.   
  41.   
  42.     <View  
  43.         android:layout_width="fill_parent"  
  44.         android:layout_height="1dp"  
  45.         android:layout_marginTop="20dp"  
  46.         android:background="#B8B894" />  
  47.   
  48.     <TextView  
  49.         android:id="@+id/textView2"  
  50.         android:layout_width="fill_parent"  
  51.         android:layout_height="wrap_content"  
  52.         android:layout_marginTop="30dp"  
  53.         android:gravity="center_horizontal"  
  54.         android:textSize="22dp"  
  55.         android:text="Radio button inside RadioGroup" />  
  56.   
  57.   
  58.     <!--   Customized RadioButtons  -->  
  59.   
  60.   
  61.     <RadioGroup  
  62.         android:layout_width="wrap_content"  
  63.         android:layout_height="wrap_content"  
  64.         android:id="@+id/radioGroup">  
  65.   
  66.         <RadioButton  
  67.             android:id="@+id/radioMale"  
  68.             android:layout_width="fill_parent"  
  69.             android:layout_height="wrap_content"  
  70.             android:text="  Male"  
  71.             android:layout_marginTop="10dp"  
  72.             android:checked="false"  
  73.             android:textSize="20dp" />  
  74.   
  75.         <RadioButton  
  76.             android:id="@+id/radioFemale"  
  77.             android:layout_width="fill_parent"  
  78.             android:layout_height="wrap_content"  
  79.             android:text="   Female"  
  80.             android:layout_marginTop="20dp"  
  81.             android:checked="false"  
  82.   
  83.             android:textSize="20dp" />  
  84.     </RadioGroup>  
  85.   
  86.     <Button  
  87.         android:layout_width="wrap_content"  
  88.         android:layout_height="wrap_content"  
  89.         android:text="Show Selected"  
  90.         android:id="@+id/button"  
  91.         android:onClick="onclickbuttonMethod"  
  92.         android:layout_gravity="center_horizontal" />  
  93.   
  94.   
  95. </LinearLayout>  
  96. File: MainActivity.java
  97. package example.javatpoint.com.radiobutton;  
  98.   
  99. import android.support.v7.app.AppCompatActivity;  
  100. import android.os.Bundle;  
  101. import android.view.View;  
  102. import android.widget.Button;  
  103. import android.widget.RadioButton;  
  104. import android.widget.RadioGroup;  
  105. import android.widget.Toast;  
  106.   
  107. public class MainActivity extends AppCompatActivity {  
  108.     Button button;  
  109.     RadioButton genderradioButton;  
  110.     RadioGroup radioGroup;  
  111.     @Override  
  112.     protected void onCreate(Bundle savedInstanceState) {  
  113.         super.onCreate(savedInstanceState);  
  114.         setContentView(R.layout.activity_main);  
  115.         radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
  116.     }  
  117.     public void onclickbuttonMethod(View v){  
  118.         int selectedId = radioGroup.getCheckedRadioButtonId();  
  119.         genderradioButton = (RadioButton) findViewById(selectedId);  
  120.         if(selectedId==-1){  
  121.             Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT).show();  
  122.         }  
  123.         else{  
  124.             Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();  
  125.         }  
  126.   
  127.     }  
  128. }  

Comments

Popular posts from this blog

SYSTEM CALLS ON OPERATING SYSTEM

TALLY ERP9