Skip to content Skip to sidebar Skip to footer

Get Radio Button Value Android

I am trying to make a simple form activity in order to learn how to use the various components supplied by Eclipse. I was doing great until I got to the radio buttons. The applica

Solution 1:

Each group of radio buttons should be in a group, I assume you did this already in your XML file.

Assuming that, then you get the ID of the checked radio button by doing:

int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();

You can do that anywhere in an Activity, or if you're using a Fragment, then you just need to put a getView() in it:

int id = ((RadioGroup)getView().findViewById( R.id.radio_group )).getCheckedRadioButtonId();

So I would change your onClick:

public void onClick(View v) {
    // Create a new Intent
    Intent i = new Intent(v.getContext(), FeedbackResults.class);

    choice = (String)county.getSelectedItem();
    int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();
    atmos = getAtmos( id );
    ...
}

private int getAtmos( int id ) {
    switch( id ) {
        case R.id.arad1:
            atmos = "1";
            break;
        case R.id.arad2:
            atmos = "2";
            break;
        case R.id.arad3:
            atmos = "3";
             break;
        case R.id.arad4:
            atmos = "4";
        break;
        case R.id.arad5:
            atmos = "5";
            break;
    }

    return atmos;
}

Post a Comment for "Get Radio Button Value Android"