Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get text of dynamically created radio button selected by the user?

Tags:

android

How can i retrieve the text of a dynamically created radio button selected by the user? Here's my code:

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); 
        // layout params to use when adding each radio button 
        LinearLayout.LayoutParams layoutParams = new 
RadioGroup.LayoutParams( 
                RadioGroup.LayoutParams.WRAP_CONTENT, 
                RadioGroup.LayoutParams.WRAP_CONTENT); 
 for (int i = 0; i < 4; i++){ 
            final RadioButton newRadioButton = new RadioButton(this); 
            c3 = db.getAns(3); 
        for (int j=0;j<i;j++) 
            c3.moveToNext(); 
           label = c3.getString(0); 
        newRadioButton.setText(label); 
        newRadioButton.setId(6); 
        radiogroup.addView(newRadioButton, layoutParams); 

Waiting for the reply, Maqsood

like image 401
Muhammad Maqsoodur Rehman Avatar asked Jun 18 '10 14:06

Muhammad Maqsoodur Rehman


3 Answers

Surprised there isn't an easier way. If you are going to do something special though based on which button you should probably checked the ID instead of the Label.

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
         void onCheckedChanged(RadioGroup rg, int checkedId) {
              for(int i=0; i<rg.getChildCount(); i++) {
                   RadioButton btn = (RadioButton) rg.getChildAt(i);
                   if(btn.getId() == checkedId) {
                        String text = btn.getText();
                        // do something with text
                        return;
                   }
              }
         }
    });
like image 133
Robby Pond Avatar answered Nov 15 '22 09:11

Robby Pond


I think there is an easier way to do this...

I just created a radio button with the id of the button checked and works fine..

The solution looks like this

RadioButton TheTextIsHere = (RadioButton) findViewById(RadioGroup.getCheckedRadioButtonId());

So now you got a RadioButton that refers to the RadioButton that is checked into the RadioGroup and then you can easily...

TheTextIsHere.getText().toString();

Hope i helped :)

like image 32
user878813 Avatar answered Nov 15 '22 09:11

user878813


An old question but this answer could maybe help someone else.

I solved the problem to get the text from a RadioButton like below without any for-loop. It work for me, but I have used xml, but think the principle would work anyways.

The code behind // is only necessary if no RadioButton is pre-check because radioBtnChecked will be -1 if no RadioButton is selected then. So the app will "crash" because findviewbyid(-1) is not valid. At least in xml you pre-check a RadioButton with android:checked="true".

RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
int radioBtnChecked = radioGroup1.getCheckedRadioButtonId();      
  // if (radioBtnChecked <= 0) {
  //   radioText = "None selected";      
  // } 
  // else {
       RadioButton rBtn = (RadioButton) findViewById(radioBtnChecked);
       radioText = rBtn.getText().toString();
like image 43
this user Avatar answered Nov 15 '22 08:11

this user