Sunday, August 9, 2015

How to create a RadioGroup programmatically in Android

activity_main.xml

<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"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#ad8fff"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create RadioGroup"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Initialize a new RadioGroup
                RadioGroup rg = new RadioGroup(getApplicationContext());
                rg.setOrientation(RadioGroup.VERTICAL);

                // Initialize the layout parameters for RadioGroup
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                // Set the RadioGroup position below the Button
                lp.addRule(RelativeLayout.BELOW, R.id.btn);

                // Apply the layout parameters for RadioGroup
                rg.setLayoutParams(lp);

                // Create a Radio Button for RadioGroup
                RadioButton rb_coldfusion = new RadioButton(getApplicationContext());
                rb_coldfusion.setText("ColdFusion");
                rb_coldfusion.setTextColor(Color.BLACK);
                rg.addView(rb_coldfusion);

                // Create another Radio Button for RadioGroup
                RadioButton rb_flex = new RadioButton(getApplicationContext());
                rb_flex.setText("Flex");
                rb_flex.setTextColor(Color.BLACK);
                rg.addView(rb_flex);

                // Create another Radio Button for RadioGroup
                RadioButton rb_flash = new RadioButton(getApplicationContext());
                rb_flash.setText("Flash");
                rb_flash.setTextColor(Color.BLACK);
                rg.addView(rb_flash);

                // Finally, add the RadioGroup to main layout
                rl.addView(rg);
            }
        });
   }
}
More android examples