Create RadioGroup dynamically in android with Radio Button

How to set radio group dynamically from MainActivity.java programming file without layout XML.

In this tutorial we are creating radio RadioGroup using programmatically method. So we are not creating RadioGroup widget using activity_main.xml file .  So here is the complete step by step tutorial for Create RadioGroup dynamically in android with Radio Button.

android-project-download-code-button

How to Create RadioGroup dynamically in android with Radio Button.

Code for MainActivity.java file.

 package com.createradiogroupdynamically_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

public class MainActivity extends Activity {

 RadioButton radiobutton1,radiobutton2,radiobutton3;
 RadioGroup dynamicRadiogroup;
 RelativeLayout relativelayout;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 dynamicRadiogroup = new RadioGroup(MainActivity.this);
 radiobutton1 = new RadioButton(MainActivity.this);
 radiobutton2 = new RadioButton(MainActivity.this);
 radiobutton3 = new RadioButton(MainActivity.this);
 relativelayout = (RelativeLayout)findViewById(R.id.relativelayout1);
 
 radiobutton1.setText("Android");
 radiobutton2.setText("PHP");
 radiobutton3.setText("WordPress");
 
 dynamicRadiogroup.addView(radiobutton1);
 dynamicRadiogroup.addView(radiobutton2);
 dynamicRadiogroup.addView(radiobutton3);
 
 RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams
 (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 
 dynamicRadiogroup.setLayoutParams(layoutparams);
 
 relativelayout.addView(dynamicRadiogroup);
 
 dynamicRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
 
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 // TODO Auto-generated method stub
 
 
 if(radiobutton1.isChecked())
 {
 Toast.makeText(MainActivity.this,"Android Selected" , Toast.LENGTH_LONG).show();
 }
 
 
 if(radiobutton2.isChecked())
 {
 Toast.makeText(MainActivity.this,"PHP Selected" , Toast.LENGTH_LONG).show();
 }
 
 
 if(radiobutton3.isChecked())
 {
 Toast.makeText(MainActivity.this,"WordPress Selected" , Toast.LENGTH_LONG).show();
 }
 
 }
 });
 
 }

}

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.createradiogroupdynamically_android_examples.com.MainActivity"
 android:id="@+id/relativelayout1" >

</RelativeLayout>

Screenshot:

Create RadioGroup dynamically in android with Radio Button

Click here to download Create RadioGroup dynamically in android with Radio Button project with source code.

Leave a Reply

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