Monday, March 15, 2021

Android RadioButton and RadioGroup

 

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>  

Activity class

File: MainActivity.java

  1. package example.javatpoint.com.radiobutton;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     Button button;  
  13.     RadioButton genderradioButton;  
  14.     RadioGroup radioGroup;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
  20.     }  
  21.     public void onclickbuttonMethod(View v){  
  22.         int selectedId = radioGroup.getCheckedRadioButtonId();  
  23.         genderradioButton = (RadioButton) findViewById(selectedId);  
  24.         if(selectedId==-1){  
  25.             Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT).show();  
  26.         }  
  27.         else{  
  28.             Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();  
  29.         }  
  30.   
  31.     }  
  32. }  

Output

android Radio Button 1
android Radio Button 2 

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile