0

I have taken radio group in the navigation view of drawer layout like this

 <android.support.design.widget.NavigationView
     app:headerLayout="@layout/header"
     app:menu="@menu/drawer">

 <RadioGroup>
      <RadioButton/>
      <RadioButton/>
 </RadioGroup>

     </android.support.design.widget.NavigationView>

I want to get the selected radio button text or id but I am not getting any logs or Toast and no errors also..

Code for getting selected radio button:

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(RadioGroup group, int checkedId) {
            int pos=rg.indexOfChild(findViewById(checkedId));
             Toast.makeText(MainActivity.this, "ID = "+String.valueOf(pos),
                     Toast.LENGTH_SHORT).show();
             Log.e("",""+pos);
         } 

I know I can achieve this by using custom list view.. But how to get this working..

Bee
  • 142
  • 17
  • set a tag to a radio button then get the tag in the above code according to position,means radiobutton1.setTag(1) and radioButton2.setTag(2) in rg. setOnCheckedChangeListener just put if condition (radiobutton1.getTag==1){// do the stuff} else {//do another stuff}; – Amit Ranjan Mar 22 '16 at 07:43

2 Answers2

0

Sorry couldn't answer in comment. Try this:

navigationView.getMenu().findItem(R.id.radioButtonId);

this will return Id of your RadioButton.

Edit:

Can you do something like this:

RadioButton radio = (RadioButton)navigationView.findViewById(R.id.radioButtonId);
  • Can you do something like this: RadioButton radio = (RadioButton)navigationView.findViewById(R.id.radioButtonId); – Lokesh Jain Mar 22 '16 at 14:05
0

Example - just like provided in developer.android

You just need to add attribute onClick in xml file like below

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

Handle the onClick event in code part

 public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • But if I m writing Log or toast in **if(checked)** part I am not getting anything.. – Bee Mar 22 '16 at 08:15