Search code examples
javaandroidradio-buttonandroid-radiogroup

Handling Radio Group and Button in Fragment


first take a look on a part of my fragment xml file `

  <LinearLayout
            android:layout_weight="1"
            android:background="#aae4e4e4"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioGroup
                android:id="@+id/group_no_1"
                android:layout_width="480dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="rbclick"
                    android:text="0" />

                <RadioButton


                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="1" />

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="2" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="3" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="4" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="5" />


                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:onClick="rbclick"
                    android:text="6" />
            </RadioGroup>


        </LinearLayout>

now take look on my fragment java class

 public class MealEntry extends Fragment {

    private MealEntryViewModel mViewModel;

    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private static final String TAG = "MealEntry";
    public RadioGroup rg;
    RadioButton rb;


    public static MealEntry newInstance() {
        return new MealEntry();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);


         rg  = view.findViewById(R.id.group_no_1);
     return view;
    }


        public void rbclick (View v){

        int radiobtnid=rg.getCheckedRadioButtonId();
        rb=(RadioButton)findViewById(radiobtnid);

    }

previously by this procedure i mean the (rbclick function ) can handle the radio group and radio button in Activity(i repeat Activity). but in Fragment the onClick method is not working. how can i resolve this? I've tried this way for fragment.

  public void rbclick (View v){

        int radiobtnid=rg.getCheckedRadioButtonId();
        rb =(RadioButton)getActivity().findViewById(radiobtnid);

    }

I think this handler is not working. cause it's got crash when i click the radio button.

here's is Log cat.

    12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.closeup, PID: 3720
    android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
        at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
        at android.view.View.performClick(View.java:4780)
        at android.widget.CompoundButton.performClick(CompoundButton.java:120)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9

Solution

  • Here is solution to handle radio group in fragment.

    1.Remove all line android:onClick="rbclick" from each RadioButton in meal_entry_fragment.xml file, and create an id for each RadioButton.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#aae4e4e4"
        android:orientation="horizontal">
    
        <RadioGroup
            android:id="@+id/group_no_1"
            android:layout_width="480dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="1" />
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />
    
            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="3" />
    
            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="4" />
    
            <RadioButton
                android:id="@+id/radio5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="5" />
    
            <RadioButton
                android:id="@+id/radio6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="6" />
        </RadioGroup>
    </LinearLayout>
    

    2.Create an instance of OnCheckedChangeListener in MealFragment class.

    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.radio0:
                    // Write your code here
                    break;
                case R.id.radio1:
                    // Write your code here
                    break;
                case R.id.radio2:
                    // Write your code here
                    break;
                case R.id.radio3:
                    // Write your code here
                    break;
                case R.id.radio4:
                    // Write your code here
                    break;
                case R.id.radio5:
                    // Write your code here
                    break;
                case R.id.radio6:
                    // Write your code here
                    break;
                default:
                    break;
            }
        }
    };
    
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
        rg = view.findViewById(R.id.group_no_1);
        rg.setOnCheckedChangeListener(onCheckedChangeListener);
        return view;
    }