Java – Android radio buttons in Fragment

Android radio buttons in Fragment… here is a solution to the problem.

Android radio buttons in Fragment

I’m working on radio buttons tutorial and want to create some RadioGroups with RadioButtons in a fragment. I defined the onClick method, but if I click RadioButton, I get an error:

java.lang.IllegalStateException: Could not find method FirstQuestion(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'test1from10question1answerA'
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                               at android.view.View.performClick(View.java:4438)
                                                                               at android.widget.CompoundButton.performClick(CompoundButton.java:100)
                                                                               at android.view.View$PerformClick.run(View.java:18422)
                                                                               at android.os.Handler.handleCallback(Handler.java:733)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:136)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5045)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                               at dalvik.system.NativeStart.main(Native Method)

My xml file is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.plan.aplikacjamobilna.registerTestFragments.question1from10"
   android:layout_marginLeft="20dp"
   android:layout_marginRight="20dp"
   android:orientation="vertical">

<!-- TODO: Update blank fragment layout -->
   <RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="20dp"
      android:layout_gravity="center_horizontal">

<TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="1:"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="A"
         android:id="@+id/test1from10question1answerA"
         android:onClick="FirstQuestion"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="B"
         android:id="@+id/test1from10question1answerB"
         android:onClick="FirstQuestion"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="C"
         android:id="@+id/test1from10question1answerC"
         android:onClick="FirstQuestion"/>
   </RadioGroup>

<RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="20dp"
      android:layout_gravity="center_horizontal">
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="2:"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="A"
         android:id="@+id/test1from10question2answerA"
         android:onClick="SecondQuestion"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="B"
         android:id="@+id/test1from10question2answerB"
         android:onClick="SecondQuestion"/>
      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="C"
         android:id="@+id/test1from10question2answerC"
         android:onClick="SecondQuestion"/>
    </RadioGroup>

<RadioGroup
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="20dp"
       android:layout_gravity="center_horizontal">
       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="3:"/>
       <RadioButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="A"
          android:id="@+id/test1from10question3answerA"
          android:onClick="ThirdQuestion"/>
       <RadioButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="B"
          android:id="@+id/test1from10question3answerB"
          android:onClick="ThirdQuestion"/>
       <RadioButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="C"
          android:id="@+id/test1from10question3answerC"
          android:onClick="ThirdQuestion"/>
   </RadioGroup>

</LinearLayout>

and the code in fragment:

public class question1from10 extends Fragment {

public question1from10() {
     Required empty public constructor
  }

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
     Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_question1from10, container, false);
  }

public void FirstQuestion(View view) {

boolean checked = ((RadioButton) view).isChecked();
    switch(view.getId()){

case R.id.test1from10question1answerA:
         Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG ).show();

case R.id.test1from10question1answerB:
         Toast.makeText(getActivity(), "B", Toast.LENGTH_LONG ).show();

case R.id.test1from10question1answerC:
         Toast.makeText(getActivity(), "C", Toast.LENGTH_LONG ).show();
    }
  }
}

Is there a problem with the RadioButtons in fragment or is my code incorrect?

Solution

When you use the android:onClick tag in XML, Android looks only for the specified onClick method in your current activity. It does not look at any fragments.

The simplest option is to assign onClick programmatically.

public class question1from10 extends Fragment implements View.OnClickListener {

public question1from10() {
         Required empty public constructor
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

 Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_question1from10, container, false);

 Set the onClick for each of our views as the one implemented by this Fragment
        rootView.findViewById(R.id.test1from10question1answerA).setOnClickListener(this);
        rootView.findViewById(R.id.test1from10question1answerB).setOnClickListener(this);
        rootView.findViewById(R.id.test1from10question1answerC).setOnClickListener(this);
        ...

return rootView;
    }

@Override
    public void onClick(View view) {

boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()){

case R.id.test1from10question1answerA:
                Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG ).show();

case R.id.test1from10question1answerB:
                Toast.makeText(getActivity(), "B", Toast.LENGTH_LONG ).show();

case R.id.test1from10question1answerC:
                Toast.makeText(getActivity(), "C", Toast.LENGTH_LONG ).show();
        }
    }
}

Related Problems and Solutions