Reputation: 34287
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
Upvotes: 5
Views: 19129
Reputation: 11
There is a hacky way to do it. For each radio button you need to have a positive integer as an ID. Then using that ID you can refer to the selected radio button. Here the code:
private void addButtons(String[] taskNames) {
//You can define your radio group this way
//or you can define it in onCreate. NOTE: if you
//define it in onCreate, make sure you do a
//rGroup.removeAllViews() or rGroup.removeView(child)
rGroup = new RadioGroup(this);
//hash code is the ID we will give to the radio buttons
int hash;
//going through the list of names and making radio buttons
//out of them and putting them into a radio group
for(String name : taskNames)
{
//making a button
RadioButton button = new RadioButton(this);
//setting the button's text
button.setText(name);
//setting the button's ID by finding it's hashCode
//Note that the ID MUST be a positive number
hash = Math.abs((name).hashCode());
button.setId(hash);
//adding to the radio button group
rGroup.addView(button);
}
//Then you can add the radio group to your desired layout from the xml file
LinearLayout desiredLayout = (LinearLayout) findViewById(R.id.desireLinearLayout);
desiredLayout.addView(rGroup);
}
//here is a how to get the checked radio button
private void onClickSubmit()
{
//for instance you can add the name to a DB
DatabaseHandler db = new DatabaseHandler(this);
try
{
//get the ID of the button (i.e. the hashCode we assigned to it
int id = rGroup.getCheckedRadioButtonId();
//Getting the radio button
RadioButton rbChecked = (RadioButton) rGroup.findViewById(id);
//getting the name of the radio button
String rbName = rbChecked.getText().toString();
//adding the name to the DB
db.addName(rbName);
//showing a friendly message to the user that the operation has been successful
Toast.makeText(this, "Yay, name added", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(this, "Can't submit", Toast.LENGTH_SHORT).show();
}
}
Hashcodes are deterministic so it's safe to use them, but since we are doing a Math.abs then we make some room for possibilities of 2 things hash to the same value because we are eliminating the negative portion. But so far it's been working fine for me. But you can do all sorts of creative things to avoid collisions. I'm sure you will figure it out :)
Upvotes: 0
Reputation: 81
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();
Upvotes: 3
Reputation: 12900
A HasMap is better in this kind of situation. HashMap's are designed to quickly retrieve values... Off course, your specific case uses 'only' 4 radio buttons, so you will not really notice the difference. Still, I would always prefer this solution
Create a member variable for the HasMap:
Map<Integer, RadioButton> mapping = new HashMap<Integer, RadioButton>();
In the for-loop, where you create your RadioButtons, add them to the hasmap:
{
... // your for-loop
int id = <your id here>
newRadioButton.setId(id); // set the id
mapping.put(id, newRadioButton); // store the id as the key-value
... // continue with your for-loop
}
Finally, in your onCheckedChangeListener, you can extract the RadioButton from the HashMap. Note: a HashMap does not loop through all it's entries to extract the value, hence it will be (a little) faster. Off course, you have to pay with memory in this case:
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup rg, int checkedId)
{
String txt = ((RadioButton)mapping.get(checkedId)).getText();
// do something with your text
}
});
Upvotes: 0
Reputation: 795
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 :)
Upvotes: 9
Reputation: 73494
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;
}
}
}
});
Upvotes: 19