Search code examples
javaandroidandroid-activityandroid-preferencespreferenceactivity

access widgets state in a DialogPreference


I have a dialog in my preferences activity defined as follows (the layout is defined in xml):

public class DialogPreference extends android.preference.DialogPreference {
    public DialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void onClick (DialogInterface dialog, int which) {
        if (DialogInterface.BUTTON_POSITIVE == which) {
            if (getKey() == 
                getContext().getResources().getText(R.string.blabla)) {
                // FIXME ??? - how ?
                Log.v(TAG, "DialogPreference: onClick");
            }
        }
    }
}

In the dialog there are several widgets, namely a RadioGroup and several RadioButtons. I currently am not able to find a way to access these widgets in the onClick method.

What is the way to access those widgets?


Solution

  • You can cast the DialogInterface to AlertDialog like so:

    public void onClick (DialogInterface dialog, int which) {
        if (DialogInterface.BUTTON_POSITIVE == which) {
            RadioButton button = (RadioButton) ((AlertDialog) dialog).findViewById(R.id.radio_button_id);
            // continue using button
        }
    }
    

    (or something similar)