Search code examples
androidradio-buttonlistener

RadioButton Listener inside a DialogFragment


I've try to implement RadioGroup view and put it inside a DialogFragment, but something gone wrong. I've follow this tutorial

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

but instead Activity, view is initialized in a DialogFragment:

public class MenuDialog extends DialogFragment {

    public MenuDialog(){}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_layout, container);
        this.getDialog().setTitle("title");
        return view;
    }

public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch(view.getId()) {
    case R.id.radio1:
        if(checked) {
            Log.i("MENU", ((RadioButton)view).getText());
        }
        break;
    case R.id.radio2:
        if(checked) {
            Log.i("MENU", ((RadioButton)view).getText());
        }
        break;
    case R.id.radio3:
        if(checked) {
            Log.i("MENU", ((RadioButton)view).getText());
        }
        break;
    case R.id.radio4:
        if(checked) {
            Log.i("MENU", ((RadioButton)view).getText());
        }
        break;
    }
    this.getDialog().dismiss();
}

}

xml:

<?xml version="1.0" encoding="utf-8"?>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/radio_group" >

    <RadioButton
        android:id="@+id/radio1"
        android:text="20" />

    <RadioButton
        android:id="@+id/radio2"
        android:text="40" />

    <RadioButton
        android:id="@+id/radio3"
        android:text="60" />

    <RadioButton
        android:id="@+id/radio4"
        android:text="80" />
</RadioGroup>

onRadioButtonClicked listener it's implemented in the same way of tutorial. Now, when launch dialog, if i tap on a radiobutton i obtain this error:

09-27 09:41:08.018: E/AndroidRuntime(7447): FATAL EXCEPTION: main
09-27 09:41:08.018: E/AndroidRuntime(7447): java.lang.IllegalStateException: Could not
 find a method onRadioButtonClicked(View) in the activity class 
android.view.ContextThemeWrapper for onClick handler on view class 
android.widget.RadioButton with id 'radio1'

What's wrong?


Solution

  • I'd rather implement the click handler inside of the fragment and add the listener dynamically (via java instead of XML). So I would do something like this:

    public class MenuDialog extends DialogFragment implements OnClickListener{
    [...]    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_layout, container);
        view.findViewById(R.id.radio1).setOnClickListener(this);
        view.findViewById(R.id.radio2).setOnClickListener(this);
        //and so on [...]
        this.getDialog().setTitle("title");
        return view;
    }
    
    @Override
    public void onClick(View v){
        [...]
    }