Search code examples
androidandroid-radiogroupandroid-radiobutton

Getting programmatically added radio button value from radio group in linear layout


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();

Solution

  • 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;
    }