Skip to content Skip to sidebar Skip to footer

How To Add Radio Button Dynamically As Per The Given Number Of Counts?

I have tried this code..It will display three radio buttons in a single row when the emulator starts. But I need a button event for this. i.e; if I click the button, it should ask

Solution 1:

Please find below the code, I have created an 'EditText' and a 'Button' in the xml layout. Input a number in the 'EditText' and click the Button , The same no. of radio buttons will be added in the Layout.

This is your ActivityMain

publicclassActivityMainextendsAppCompatActivityimplementsView.OnClickListener {

    EditText mEtNumOfRadioBtns;
    Button mBtnAdd;
    StringTAG = "TestActivity";
    RadioGroup mRgAllButtons;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        mEtNumOfRadioBtns = findViewById(R.id.et_no);
        mBtnAdd = findViewById(R.id.btn);
        mRgAllButtons = findViewById(R.id.radiogroup);
        //
        mBtnAdd.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                int number = Integer.parseInt(mEtNumOfRadioBtns.getText().toString().trim());
                addRadioButtons(number);
            }
        });
    }

    publicvoidaddRadioButtons(int number) {
        mRgAllButtons.setOrientation(LinearLayout.HORIZONTAL);
        //for (int i = 1; i <= number; i++) {
            RadioButton rdbtn = newRadioButton(this);
            rdbtn.setId(View.generateViewId());
            rdbtn.setText("Radio " + rdbtn.getId());
            rdbtn.setOnClickListener(this);
            mRgAllButtons.addView(rdbtn);
        }
    }

    @OverridepublicvoidonClick(View v) {
        Log.d(TAG, " Name " + ((RadioButton)v).getText() +" Id is "+v.getId());
    }
}

And here is your layout file with name 'activity_main'

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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"tools:context=".MainActivity" ><RadioGroupandroid:id="@+id/radiogroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:orientation="vertical" /><LinearLayoutandroid:layout_marginTop="20dp"android:layout_marginLeft="20dp"android:layout_height="wrap_content"android:layout_width="match_parent"><EditTextandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:hint="Enter no."android:inputType="number"android:id="@+id/et_no"/><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:text="Add Radio btn"android:id="@+id/btn"/></LinearLayout></RelativeLayout>

Solution 2:

Try something like below:

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;

for(int i=0;i<3;i++){
      RadioButtonradioButton=newRadioButton(this);
      radioButton.setText("new"+i);
      radioButton.setId(View.generateViewId());
      rprms= newRadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      rgp.addView(radioButton, rprms);
}

Solution 3:

This is the way to do this:

RadioGrouprgp= (RadioGroup) findViewById(R.id.radio_group);        
    intbuttons=5;
    for (inti=1; i <= buttons ; i++) {
        RadioButtonrbn=newRadioButton(this);
        rbn.setId(View.generateViewId());
        rbn.setText("RadioButton" + i);
        rgp.addView(rbn);
    }

enter image description here

But what if you need to do this horizontally, just add the orientation (the default value is vertical) with setOrientation() method:

RadioGrouprgp= (RadioGroup) findViewById(R.id.radio_group);
    rgp.setOrientation(LinearLayout.HORIZONTAL);
    intbuttons=5;
    for (inti=1; i <= buttons; i++) {
        RadioButtonrbn=newRadioButton(this);
        rbn.setId(View.generateViewId());
        rbn.setText("RadioButton" + i);
        rbn.setLayoutParams(params);
        rgp.addView(rbn);
    }

enter image description here


This is the complete code:

first of all defining inside our Layout the RadioGroup:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"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"><RadioGroupandroid:id="@+id/radio_group"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

The code into the MainActivity:

publicclassMainActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Defining 5 buttons.intbuttons=5;
        AppCompatRadioButton[] rb = newAppCompatRadioButton[buttons];

        RadioGrouprgp= (RadioGroup) findViewById(R.id.radio_group);
        rgp.setOrientation(LinearLayout.HORIZONTAL);

        for (inti=1; i <= buttons; i++) {
            RadioButtonrbn=newRadioButton(this);
            rbn.setId(View.generateViewId());
            rbn.setText("RadioButton" + i);
            LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
            rbn.setLayoutParams(params);
            rgp.addView(rbn);
        }

    }
}

Solution 4:

Add a Button and EditText in xml and take input from editText to variable inputValue and try this,

publicclassMyActivityextendsActivity {

    /**
     * Called when the activity is first created.
     */
    LinearLayout ll=null;
    ViewGroup   vwgroup;
    Button btnClick;    

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vwgroup=((ViewGroup)findViewById(R.id.radiogroup)
        btnClick=(Button)findViewById(R.id.button1);


        btnClick.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View arg0) {
                if(ll!=null)
                    viewgroup.removeView(ll);
                ll = newLinearLayout(this);
                for(inti=1; i < inputValue; i++) {
                    RadioButtonrdbtn=newRadioButton(this);
                    rdbtn.setId(View.generateViewId());
                    rdbtn.setText("Radio " + rdbtn.getId());
                    ll.addView(rdbtn);
                }
                vwgroup.addView(ll);

            }
        });
    }
}

Post a Comment for "How To Add Radio Button Dynamically As Per The Given Number Of Counts?"