bhavdip
bhavdip

Reputation: 189

how to get id of checked radio from radio group

i have created dynamic radio group in that each group have three radio button so on checked change listener how can i get id of radio button checked in radio group.....thanks in advance..

    public class Rate_me_up extends Activity implements OnClickListener,
        OnCheckedChangeListener {
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rate_me_up);
        layout = (LinearLayout) findViewById(R.id.linear);
        Button btnp = (Button) findViewById(R.id.buttonp);
        btnp.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
        radioGroup.setOrientation(1);

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(radioGroup, p);
        RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
        radioButtonView.setText("RadioButton");
        radioButtonView.setChecked(false);
        radioGroup.addView(radioButtonView, p);

        RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
        radioButtonView2.setText("RadioButton2");
        radioButtonView2.setChecked(false);
        radioGroup.addView(radioButtonView2, p);
        RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
        radioButtonView3.setText("RadioButton2");
        radioGroup.addView(radioButtonView3, p);
        radioButtonView3.setChecked(false);
        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }

}

Upvotes: 2

Views: 4727

Answers (6)

Ana
Ana

Reputation: 831

The field checkId in

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}

will give you checked radioButton's id.

edit:

do it like this:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

switch(checkedId){
   case R.id.radio1: //assumed id of radioButton1
   //statements
   break;

   case R.id.radio2:
   .
   break; //so on
 }
}

for dynamic radioButtons compare by fetching id from there insatnce like radioButton1.getId();

Upvotes: 0

Mehul Joisar
Mehul Joisar

Reputation: 15358

You need to set the id before you try to get id . Use following piece of code.

   @Override
public void onClick(View v) {
    RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
    radioGroup.setOrientation(1);

    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    layout.addView(radioGroup, p);
    RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(1);
    radioButtonView.setText("RadioButton");
    radioButtonView.setChecked(false);
    radioGroup.addView(radioButtonView, p);

    RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(2);
    radioButtonView2.setText("RadioButton2");
    radioButtonView2.setChecked(false);
    radioGroup.addView(radioButtonView2, p);
    RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(3);
    radioButtonView3.setText("RadioButton2");
    radioButtonView3.setChecked(false);
    radioGroup.addView(radioButtonView3, p);
    radioGroup.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(Rate_me_up.this, "id : "+checkedId, Toast.LENGTH_SHORT).show();//or you can also get the id by using group.getCheckedRadioButtonId()
}

Upvotes: 0

MetaldroiD
MetaldroiD

Reputation: 436

Convert Radiobutton Value into a String.

This is how you need to try,

On onClick:

genradiocheck();
radiovalue =  radiobutton1.getText().toString();

`

    private boolean genradiocheck()
  {
    boolean gender_flag = false;

    if (r_group.getCheckedRadioButtonId() == -1) 
    {
      //
    } 
    else 
    {
        radiobutton1 = (RadioButton) findViewById(r_group
                  .getCheckedRadioButtonId());

     gender_flag = true;
    }

    return gender_flag;
  }

Hope this helps.

Upvotes: 1

Spring Breaker
Spring Breaker

Reputation: 8251

First set the id of the RadioButton through setId() .Do something like the following,

@Override
        public void onClick(View v) {
            RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
            radioGroup.setOrientation(1);

            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.addView(radioGroup, p);
            RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(1);
            radioButtonView.setText("RadioButton");
            radioButtonView.setChecked(false);
            radioGroup.addView(radioButtonView, p);

            RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(2);
            radioButtonView2.setText("RadioButton2");
            radioButtonView2.setChecked(false);
            radioGroup.addView(radioButtonView2, p);

            RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(3);
            radioButtonView3.setText("RadioButton2");
            radioGroup.addView(radioButtonView3, p);
            radioButtonView3.setChecked(false);
            radioGroup.setOnCheckedChangeListener(this);

        }

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
                case 1:
                    // first radiobutton is clicked
                    break;
                case 2:
                    // second radiobutton is clicked
                    break;
                case 3:
                    // third radiobutton is clicked
                    break;


                default:
                    break;
        }

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62391

This will be help you:

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));

Upvotes: 0

sr.farzad
sr.farzad

Reputation: 665

switch (radioXO.getCheckedRadioButtonId()) {
            case R.id.rdo_X:
                player1Character = "X";
                break;
            case R.id.rdo_O:
                player1Character = "O";
                break;

            }

Upvotes: 0

Related Questions