Actually I am creating a trivia app. For creating questions, I need to assign the options to radio buttons in a radio group, which has to be done dynamically. For giving the options, i want to enter the option in an editText and grab it into radio group on clicking a button. This has to be done for every option which has to be placed in the radio group. Please help me.
Asked
Active
Viewed 572 times
-2
-
possible duplicate of [Dynamically Change the Text of RadioButton](http://stackoverflow.com/questions/11013835/dynamically-change-the-text-of-radiobutton) – Dhinakaran Thennarasu Feb 04 '15 at 03:25
-
My assignment is regarding pulling the information from edit Text field and assigning it to radio buttons each item clicked on a button. I am told to do this way because we are creating questions and options for those in an app. – Varun Varma Feb 07 '15 at 02:02
2 Answers
1
RadioButton
extends Textview
basically, which means you can call getText() and setText (CharSequence text) on any object instance of RadioButton
to get text from EditText
is EditText.getText().toString()
all these widget's mother is TextView
Helpful??

Elltz
- 10,730
- 4
- 31
- 59
-
Yes. I already knew this. But this scenario was not applicable for the radio button text setting from edit text – Varun Varma Feb 07 '15 at 02:03
-
what?? i dont get you, all i am saying is do this `RadioButton.setText(EditText.getText().toString());` and you are saying it is not applicable? how @VarunVarma – Elltz Feb 07 '15 at 02:22
0
Use edittext.addTextChangedListener() and onaftertextchange call radioButton.setText(edittext.getText().toString());
Code
uid = (EditText) findViewById(R.id.ed_UserId);
radioGroup = (RadioGroup) findViewById(R.id.radioInstall);
uid.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(selectedId);
radioButton.setText(uid.getText().toString());
if (radioButton.getText().equals("Install Now")) {
Toast.makeText(getApplicationContext(),
radioButton.getText(), Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(),
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});

Fasiha
- 488
- 3
- 11