Select radio button in radiogroup in android programmatically

How to check item(radio button) from radiogroup dynamically using button click.

In this tutorial we are creating a radio group with two radio buttons inside it.Now we are creating two another normal buttons to select the radio button value dynamically on button click. So here is the complete step by step tutorial for Select radio button in radiogroup in android programmatically.

android-project-download-code-button

How to Select radio button in radiogroup in android programmatically.

Code for MainActivity.java file.

 package com.selectradiobuttonradiogroup_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

 RadioGroup radiogroup;
 Button SelectPHP, SelectAndroid;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 radiogroup = (RadioGroup)findViewById(R.id.radiogroup);
 
 SelectAndroid = (Button)findViewById(R.id.button1);
 
 SelectPHP = (Button)findViewById(R.id.button2);
 
 SelectAndroid.setOnClickListener( new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Pass id of radio button which you want to select.
 radiogroup.check(R.id.radioButton1);
 }
 });
 
 SelectPHP.setOnClickListener( new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Pass id of radio button which you want to select.
 radiogroup.check(R.id.radioButton2);
 
 }
 });
 
 }

}

Code for activity_main.xml layout file.

<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.selectradiobuttonradiogroup_android_examples.com.MainActivity" >

 <RadioGroup
 android:id="@+id/radiogroup"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 >
 
 <RadioButton
 android:id="@+id/radioButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Android" />

 <RadioButton
 android:id="@+id/radioButton2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/radioButton1"
 android:layout_below="@+id/radioButton1"
 android:text="PHP" />
 
 </RadioGroup>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/radiogroup"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="17dp"
 android:text="Click Here to Select Android" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Click Here to Select PHP" />

</RelativeLayout>

Screenshot:

Select radio button in radiogroup in android programmatically

Click here to download Select radio button in radiogroup in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *