Android RadioGroup



  1. Get selected RadioButton Id in RadioGroup.
  2. Set TextView TextColor using RadioGroup Buttons.
  3. Dynamic RadioGroup Creation in Android.


1.Get selected RadioButton Id in RadioGroup.


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 


Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <RadioGroup  
     android:id="@+id/radioGroup1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:layout_marginLeft="48dp"  
     android:layout_marginTop="82dp" >  
     <RadioButton  
       android:id="@+id/radio0"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:checked="true"  
       android:text="Hemanth" />  
     <RadioButton  
       android:id="@+id/radio1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Somaraju" />  
     <RadioButton  
       android:id="@+id/radio2"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Gudivada" />  
   </RadioGroup>  
 </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.widget.RadioGroup;  
 import android.widget.RadioGroup.OnCheckedChangeListener;  
 import android.widget.Toast;  
 public class MainActivity extends Activity {  
  RadioGroup rg;  
  int pos;  
  int pos1;  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     rg = (RadioGroup) findViewById(R.id.radioGroup1);  
     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  @Override  
  public void onCheckedChanged(RadioGroup group, int checkedId) {  
   // TODO Auto-generated method stub  
   // Method 1 For Getting Index of RadioButton  
   pos=rg.indexOfChild(findViewById(checkedId));  
     Toast.makeText(getBaseContext(), "Kakinada = "+String.valueOf(pos),  
   Toast.LENGTH_SHORT).show();  
     //Method 2 For Getting Index of RadioButton  
    pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));  
     Toast.makeText(getBaseContext(), "India = "+String.valueOf(pos1),  
   Toast.LENGTH_SHORT).show();  
     switch (pos)  
     {  
     case 0 :  
   Toast.makeText(getBaseContext(), "You have Clicked Hemanth",  
    Toast.LENGTH_SHORT).show();  
     break;  
     case 1 :  
     Toast.makeText(getBaseContext(), "You have Clicked Somaraju",  
    Toast.LENGTH_SHORT).show();  
     break;  
     case 2 :  
     Toast.makeText(getBaseContext(), "You have Clicked Gudivada",  
    Toast.LENGTH_SHORT).show();  
     break;  
   default :  
   //The default selection is RadioButton 1  
     Toast.makeText(getBaseContext(),"You have Clicked Hemanth" ,  
    Toast.LENGTH_SHORT).show();  
     break;  
     }  
  }  
  });  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.main, menu);  
     return true;  
   }  
 }  

Step 4 : 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.gudivada.hemanthsomaraju"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.gudivada.hemanthsomaraju.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Step : Our output will be like this : 



2.Set TextView TextColor using RadioGroup Buttons.

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <RadioGroup  
     android:id="@+id/radioGroup1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerInParent="true" >  
     <RadioButton  
       android:id="@+id/radio0"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:checked="true"  
       android:text="Red" />  
     <RadioButton  
       android:id="@+id/radio1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Green" />  
     <RadioButton  
       android:id="@+id/radio2"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Blue" />  
   </RadioGroup>  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="53dp" />  
 </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code : 


 package com.gudivada.hemanthsomaraju;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.graphics.Color;  
 import android.view.Menu;  
 import android.widget.RadioButton;  
 import android.widget.RadioGroup;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import android.widget.RadioGroup.OnCheckedChangeListener;  
 public class MainActivity extends Activity {  
  RadioGroup rg;  
  RadioButton r1;  
  TextView t1;  
  int pos;  
  int pos1;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     rg= (RadioGroup) findViewById(R.id.radioGroup1);  
     t1=(TextView) findViewById(R.id.textView1);  
     t1.setText("Hemanth Somaraju");  
     t1.setTextSize(15);  
     r1=(RadioButton) findViewById(R.id.radio0);  
     r1.setChecked(false);  
     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  @Override  
  public void onCheckedChanged(RadioGroup group, int checkedId) {  
   // TODO Auto-generated method stub  
   //You can use any one of method...   
   // Method 1  
   pos=rg.indexOfChild(findViewById(checkedId));  
     //Method 2  
     pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));  
     switch (pos)  
     {  
     case 0 :  
     r1.setChecked(true);  
     Toast.makeText(getBaseContext(), "You have Red Color",  
    Toast.LENGTH_SHORT).show();  
     t1.setTextColor(Color.RED);  
     break;  
     case 1 :  
     Toast.makeText(getBaseContext(), "You have Green Color",  
    Toast.LENGTH_SHORT).show();  
     t1.setTextColor(Color.GREEN);  
     break;  
     case 2 :  
     Toast.makeText(getBaseContext(), "You have Blue Color",  
    Toast.LENGTH_SHORT).show();  
     t1.setTextColor(Color.BLUE);  
     break;  
   default :  
   //The default selection is RadioButton 1  
     Toast.makeText(getBaseContext(),"You have Red Color" ,  
    Toast.LENGTH_SHORT).show();  
     t1.setTextColor(Color.RED);  
     break;  
     }  
  }  
  });  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.main, menu);  
     return true;  
   }    
 }  

Step 4 : 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.gudivada.hemanthsomaraju"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.gudivada.hemanthsomaraju.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Step : Our output will be like this : 

 



3.Dynamic RadioGroup Creation in Android.


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code:




 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/rl"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <TextView  
     android:id="@+id/textView1"  
     android:textSize="20dp"  
     android:textColor="#FF0000"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="40dp"  
     android:text="Dynamic RadioGroup" />  
 </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.widget.RadioButton;  
 import android.widget.RadioGroup;  
 import android.widget.RelativeLayout;  
 import android.widget.Toast;  
 import android.widget.RadioGroup.OnCheckedChangeListener;  
 import android.widget.RelativeLayout.LayoutParams;  
 public class MainActivity extends Activity {  
  RelativeLayout rl;  
  RadioGroup rg;  
  RadioButton rb1;  
  RadioButton rb2;  
  RadioButton rb3;  
  int pos;  
  int pos1;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     rl =(RelativeLayout) findViewById(R.id.rl);  
     rg = new RadioGroup(this);  
     rb1=new RadioButton(this);  
     rb2=new RadioButton(this);  
     rb3=new RadioButton(this);  
     rb1.setText("Hemanth");  
     rb2.setText("Somaraju");  
     rb3.setText("Gudivada");  
     rg.addView(rb1);  
     rg.addView(rb2);  
     rg.addView(rb3);  
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams  
      ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);  
     params.leftMargin = 170;  
     params.topMargin = 180;  
     rg.setLayoutParams(params);  
     rl.addView(rg);  
     rb1.setChecked(true);  
     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
      @Override  
      public void onCheckedChanged(RadioGroup group, int checkedId) {  
      // TODO Auto-generated method stub  
      //You can use any one of method...   
      // Method 1  
      pos=rg.indexOfChild(findViewById(checkedId));  
      //Method 2  
      pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));  
      switch (pos)  
      {  
      case 0 :  
       Toast.makeText(getBaseContext(), "You have Clicked Hemanth",  
    Toast.LENGTH_SHORT).show();  
       break;  
      case 1 :  
       Toast.makeText(getBaseContext(), "You have Clicked Somaraju",  
    Toast.LENGTH_SHORT).show();  
       break;  
      case 2 :  
       Toast.makeText(getBaseContext(), "You have Clicked Gudivada",  
    Toast.LENGTH_SHORT).show();  
       break;  
      default :  
       //The default selection is RadioButton 1  
       Toast.makeText(getBaseContext(),"You have Clicked Hemanth" ,  
    Toast.LENGTH_SHORT).show();  
        break;  
      }  
      }  
     });  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.main, menu);  
     return true;  
   }    
 }  

Step 4 : 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.gudivada.hemanthsomaraju"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.gudivada.hemanthsomaraju.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Step : Our output will be like this :