1

I am trying to get the value of a radio button that I add to a radio group and that gets added to a linear layout, and then I call the class from my activity. This is my code:

MultiChoice.java

public class MultipleChoice {

Context context;
List<String> choice_values;
String hint;

public MultipleChoice (Context context, String hint,  List<String> choice_value_array){
    choice_values = new ArrayList<String>();
    this.context = context;
    this.choice_values = choice_value_array;
    this.hint = hint;
}

public View createRadioGroup(){
    LinearLayout llContainer = new LinearLayout(context);
    llContainer.setOrientation(LinearLayout.VERTICAL);
    llContainer.addView(hintTextView());
    llContainer.addView(radioButtons());
    return llContainer;
}

private TextView hintTextView() {
    // TODO Auto-generated method stub
    TextView tvHint = new TextView(context);
    tvHint.setText(hint);
    return tvHint;
}

private RadioGroup radioButtons() {
    // TODO Auto-generated method stub
    RadioGroup rbGroup = new RadioGroup(context);
    rbGroup.setOrientation(RadioGroup.VERTICAL);
    for (String value : choice_values){
        RadioButton rbValue = new RadioButton(context);
        rbGroup.addView(rbValue);
        rbValue.setText(value);
    }
    return rbGroup;
}
}

This is how I create the control in my activity:

LinearLayout template_container = (LinearLayout) findViewById(R.id.llTemplate);
MultipleChoice mcControl = new MultipleChoice(getApplicationContext(), parts[0], choices);
control = mcControl.createRadioGroup();
template_container.addView(control);

I have tried something like this, but I'm not sure that I am trying the correct approach since it does not work:

View child = template_container.getChildAt(i);
LinearLayout v = ((LinearLayout)child);
View rgView = v.getChildAt(1);
RadioGroup rg = ((RadioGroup)rgView);

The RadioGroup is added and displayed fine. All I want to do is get the value of the selected radio button. Thanks in advance!

EDIT

This is how I get the value of an EditText and it works fine.

I get the control and add it to a List containing Views and then I do this with it to ghet the value if the view contains an EditText:

String text = ((EditText)view).getText().toString().trim();
Lunchbox
  • 1,538
  • 2
  • 16
  • 42

4 Answers4

0

This may be really helpful: http://developer.android.com/guide/topics/ui/controls/radiobutton.html#HandlingEvents

You may need to add an id to your radio buttons.

thomasg
  • 571
  • 4
  • 17
0

You can set an id on the radio button programatically. PLease check here

Android: View.setID(int id) programmatically - how to avoid ID conflicts?

And then use findviewbyId to get the radio button

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24
  • I get that, but I want to first get the radio group from the view, and then the radio buttons, and then the values if that is at all possible. It seems to work fine with an edit text. See my edit – Lunchbox Feb 26 '14 at 11:46
0

You can try this: First add an id to radiogroup using: android:id="@+id/radiogroup"

RadioGroup rbGroup = (RadioGroup)findViewById(R.id.radiogroup);

int RadioButtonId=rbGroup.getCheckedRadioButtonId();
        View radioButton = rbGroup.findViewById(RadioButtonId);

        String = Integer.toString(rbGroup.indexOfChild(radioButton)+1);

you can also add ID programatically using:

View.setId(int id);
ofnowhere
  • 1,114
  • 2
  • 16
  • 40
0

I solved the problem by adding a onCheckChanged listener to the radio buttons on creation and then saved the selected value to shared preferences. When I needed the values I just got all the shared preferences by iterating through the ids that I used as the key in the shared prefs. My code:

private RadioGroup radioButtons(final int radio_id) {
    // TODO Auto-generated method stub
    RadioGroup rbGroup = new RadioGroup(context);
    rbGroup.setOrientation(RadioGroup.VERTICAL);
    for (final String value : choice_values) {
        RadioButton rbValue = new RadioButton(context);
        rbGroup.addView(rbValue);
        rbValue.setText(value);
        rbValue.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                savePreferences("" + radio_id, value);
            }

        });
    }
    return rbGroup;
}



private void savePreferences(String key, String value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

And getting the values:

int radio_check = 0;
for (View view : addedControls) {
            String entered_value = getControlValue(view, radio_check);
            radio_check++;
        }

In my getValue() method:

text = loadSavedPreferences("" + (radio_check));

LoadPrefs method:

private String loadSavedPreferences(String key) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String name = sharedPreferences.getString(key, "Default");
    return name;
}
Lunchbox
  • 1,538
  • 2
  • 16
  • 42