Question

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?

Was it helpful?

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)

OTHER TIPS

Below is my whole code how I do it

final AlertDialog.Builder Main_Dialog = new AlertDialog.Builder(this);
            ////This is how to set layout
            final View layout = getLayoutInflater().inflate(R.layout.conpassword,null);
    Main_Dialog.setTitle("Choose reminder");
    Main_Dialog.setView(layout);


    Main_Dialog.setTitle("Enter IP Address:");
    Main_Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int whichButton)
        {
            connectWIFi();
            if (connect && ni.getType() == ConnectivityManager.TYPE_WIFI)
            {
                                    final ImageButton input = (ImageButton) layout.findViewById(R.id.ImageButton01);
                saveIP(input.getText().toString(), deviceId);

                Thread cThread = new Thread(new ClientServer(LoginScreen.this, input.getText().toString(),
                        deviceId, mHandler));
                cThread.start();

                dialog.dismiss();
            }
            else
            {
                Builder builder = new AlertDialog.Builder(LoginScreen.this);
                builder.setTitle("Alert !");
                builder.setMessage("No WiFi connection. Please check your WiFi settings");
                builder.setPositiveButton("Ok", null);
                builder.show();
            }
        }
    });
    Main_Dialog.show();

I dont know why you are not getting getLayoutInflater but this code runs smooth with me

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top