Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding radio button list according to string values in Android Studio?

I have seen that this can be done for ListView where you can just grab the string-array values from strings.xml and populate the ListView dynamically depending on the number of listed items.

But I was wondering if the same feat can be done using Radio Buttons and Radio Button Group?

I've come across this piece of code

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("rbtn"+i);
      rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      rgp.addView(radioButton, rprms);

  }

And was wondering how I can modify the radioButton.setId("rbtn"+i) to grab string-array values from the strings.xml file instead.

like image 235
5120bee Avatar asked Jun 01 '26 12:06

5120bee


1 Answers

Use String[] foo_array = getResources().getStringArray(R.array.foo_array); to read array from strings.xml. Then loop through the obtained array.

like image 85
Nabin Bhandari Avatar answered Jun 03 '26 01:06

Nabin Bhandari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!