Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 for number of radio buttons. then If I give the count, it must display the radio buttons based on the count given. For example, If I give the count as 3, it must display the three radio buttons in a single row. Your help is highly appreciated. Thanks in advance.

  public class MyActivity extends Activity {     /**      * Called when the activity is first created.      */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         for(int row=0; row < 1; row++)         {             LinearLayout ll = new LinearLayout(this);             ll.setOrientation(LinearLayout.HORIZONTAL);             for(int i = 1; i < 4; i++) {                 RadioButton rdbtn = new RadioButton(this);                 rdbtn.setId((row * 2) + i);                 rdbtn.setText("Radio " + rdbtn.getId());                 ll.addView(rdbtn);             }             ((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);         }     }     } 

this is xml

<?xml version="1.0" encoding="utf-8"?> <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"                 tools:context=".MainActivity" >      <RadioGroup             android:id="@+id/radiogroup"             android:orientation="vertical"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_centerHorizontal="true"             android:layout_centerVertical="true"/>      </RelativeLayout> 
like image 947
Dinesh Kumar Avatar asked Oct 15 '13 11:10

Dinesh Kumar


People also ask

How do you add radio buttons to groups?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

What do you do to a group of radio button menu items so that only one of them can be selected at a time?

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.


2 Answers

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

public class ActivityMain extends AppCompatActivity implements View.OnClickListener {      EditText mEtNumOfRadioBtns;     Button mBtnAdd;     String TAG = "TestActivity";     RadioGroup mRgAllButtons;      @Override     protected void onCreate(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(new View.OnClickListener() {             @Override             public void onClick(View v) {                 int number = Integer.parseInt(mEtNumOfRadioBtns.getText().toString().trim());                 addRadioButtons(number);             }         });     }      public void addRadioButtons(int number) {         mRgAllButtons.setOrientation(LinearLayout.HORIZONTAL);         //         for (int i = 1; i <= number; i++) {             RadioButton rdbtn = new RadioButton(this);             rdbtn.setId(View.generateViewId());             rdbtn.setText("Radio " + rdbtn.getId());             rdbtn.setOnClickListener(this);             mRgAllButtons.addView(rdbtn);         }     }      @Override     public void onClick(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"?> <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"     tools:context=".MainActivity" >      <RadioGroup         android:id="@+id/radiogroup"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerHorizontal="true"         android:layout_centerVertical="true"         android:orientation="vertical" />      <LinearLayout         android:layout_marginTop="20dp"         android:layout_marginLeft="20dp"         android:layout_height="wrap_content"         android:layout_width="match_parent">          <EditText android:layout_height="wrap_content"             android:layout_width="wrap_content"             android:hint="Enter no."             android:inputType="number"             android:id="@+id/et_no"/>          <Button             android:layout_height="wrap_content"             android:layout_width="wrap_content"             android:text="Add Radio btn"             android:id="@+id/btn"/>      </LinearLayout>      </RelativeLayout> 
like image 91
Jitender Dev Avatar answered Oct 13 '22 01:10

Jitender Dev


Try something like below:

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup); RadioGroup.LayoutParams rprms;  for(int i=0;i<3;i++){       RadioButton radioButton = new RadioButton(this);       radioButton.setText("new"+i);       radioButton.setId(View.generateViewId());       rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       rgp.addView(radioButton, rprms); } 
like image 22
Sagar Maiyad Avatar answered Oct 13 '22 02:10

Sagar Maiyad