Fundamentals
Introduction
1st Android Application

OOPS Concepts
Class
Object
Abstraction
Encapsulation

Polymorphism
Inheritance
Message Passing
Dynamic Binding

UI Controls
TextView
Button
Toggle Button
CheckBox
Radio Button
Spinner
ProgressBar
SeekBar
RadioGroup
RatingBar
Switch
EditText
AutoComplete
TextView

MultiAutoComplete
TextView

ImageView
ImageButton
Gallery
MediaPlayer
VideoView
MediaController

TimePicker
DatePicker
CalendarView
Chronometer
Analog Clock
Digital Clock

Layouts
LinearLayout
RelativeLayout
GridLayout
TableLayout
FrameLayout
Fragment

Composite
ListView
ExpandableListView
GridView
ScrollView
SearchView
Sliding Drawer
Tab Widget
WebView

Transitions
Image Switcher
StackView
TextSwitcher
ViewAnimator
View Flipper
View Switcher

Advanced
Request Focus
View
View Stub
TextureView
Gesture Overlay View

SurfaceView
NumberPicker
Zoom Button
Zoom Controls
AbsoluteLayout

Database
SharedPreference
SQLite DB
MySQL DB

Webservices
Check HttpURLConnection
Single Webservice Call
Multi Webservice Call
ListView from Webservice
SQLite from Webservice
Data from Database

Telephone Manager
IMEI
IMSI
Phone Type
Sim Type
Network Type

Content Providers
Contact Provider
BookMark Provider
Own Provider


Message & Networking
Send SMS
SMS with Reports
Group SMS

Send E-Mail
Download Binary Data


Location Based Service
Display Google Map
Events with Google Map

Android Services
Create Service
Create Longtime Service

Asynchronous Task
Repeating Services
IntentService

Activity from a Service
Bind Activity to Service


Sample Projects
HMS
CNB
Login Screen
Camera Event
Browse Option
QR Code Events
Accelerometer Sensor
EditText from Spinner

Android RadioGroup

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

Note                     Download ADT Plugin Here.
Here I am using,
OS : Linux (Ubuntu 12.04)
Eclipse : Juno (Version 4.2.0)
Android API Level : 3 to 'n' as per need
Emulator API Level : It will be displayed in output image

A. 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="RadioButton 1" />
 
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton 2" />
 
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton 3" />

    </RadioGroup>
 
</RelativeLayout>

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

package balaji.radiogroup_get_radio_id;
 
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(), "Method 1 ID = "+String.valueOf(pos),
				Toast.LENGTH_SHORT).show();
	 
		    	//Method 2 For Getting Index of RadioButton
		   	pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));

		    	Toast.makeText(getBaseContext(), "Method 2 ID = "+String.valueOf(pos1),
				Toast.LENGTH_SHORT).show();
		    
		    	switch (pos)
		    	{
		    	case 0 :
				Toast.makeText(getBaseContext(), "You have Clicked RadioButton 1",
					Toast.LENGTH_SHORT).show();
			    	break;
		    	case 1 :
			    	Toast.makeText(getBaseContext(), "You have Clicked RadioButton 2",
					Toast.LENGTH_SHORT).show();
			    	break;
		    	case 2 :
			    	Toast.makeText(getBaseContext(), "You have Clicked RadioButton 3",
					Toast.LENGTH_SHORT).show();
			    	break;
			default :
				//The default selection is RadioButton 1
			    	Toast.makeText(getBaseContext(),"You have Clicked RadioButton 1" ,
					Toast.LENGTH_SHORT).show();
			    	break;
		    	}
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.radiogroup_get_radio_id"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 5 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">RadioGroup_Get_Radio_Id</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 6 : Our output will be like this :


Output


Output


Output









B. 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 balaji.radiogroup_set_color;
 
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("Hello Jelly Bean");
        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.activity_main, menu);
        return true;
    }    
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.radiogroup_set_color"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 5 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">RadioGroup_Set_Color</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 6 : Our output will be like this :


Output


Output


Output









C. 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 balaji.radiogroup_dynamic_default_selection;
 
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("Radio 1");
        rb2.setText("Radio 2");
        rb3.setText("Radio 3");
        
        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 Radio 1",
					Toast.LENGTH_SHORT).show();
        			break;
        		case 1 :
        			Toast.makeText(getBaseContext(), "You have Clicked Radio 2",
					Toast.LENGTH_SHORT).show();
        			break;
        		case 2 :
        			Toast.makeText(getBaseContext(), "You have Clicked Radio 3",
					Toast.LENGTH_SHORT).show();
        			break;
        		default :
        			//The default selection is RadioButton 1
        			Toast.makeText(getBaseContext(),"You have Clicked Radio 1" ,
					Toast.LENGTH_SHORT).show();
   			        break;
        		}
        	}
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.radiogroup_dynamic_default_selection"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 5 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">RadioGroup_Dynamic_Default_Selection</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 6 : Our output will be like this :


Output


Output









SHARE THIS PAGE



product 2

product 3

product 4

Feedbacks : balaji.scz@gmail.com