Yvan1263
Yvan1263

Reputation: 80

Retrieve Value of RadioButton checked

Since several days, I search how to retrieve the checked radio Button ID, and after display the RadioButton checked in a Toast. But I get NULLPOINTER EXECEPTION for the int ID_LANGUE. I use this code :

enter code here
int ID_LANGUE = radioGroup_LANGUE.getCheckedRadioButtonId();
                            RadioButton rb_L = (RadioButton)findViewById(ID_LANGUE);
                            if (rb_L.equals("Anglais")){
                                Toast.makeText(MainActivity.this,"Anglais",Toast.LENGTH_LONG).show();
                            }

Upvotes: 1

Views: 146

Answers (1)

Navoneel Talukdar
Navoneel Talukdar

Reputation: 4588

You have to get the radio group first by

 RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);

Then you can use setOnCheckedChangeListener to determine which radio button was clicked actually.

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.radio0:
                        //you selected radio0
                    break;    
                    case R.id.radio1:
                        //you selected radio1
                    break;  
                }
            }
        });

Other way is using getCheckedRadioButtonId also int id = radioGroup.getCheckedRadioButtonId(); you get hold of selected radio id.

Upvotes: 2

Related Questions