Show dialog from onPreferenceClick method

129 views Asked by At

I want to display a dialog to the user when she click a preference. I use the following code, from the onPreferenceClick method, which cause the app to stop -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);

    Preference pr = findPreference("advisorsKey");

    pr.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {


            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(getBaseContext());
            dlgAlert.setMessage("This is an alert with no consequence");
            dlgAlert.setTitle("App Title");
            dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            dlgAlert.setCancelable(true);

            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //dismiss the dialog

                        }
                    });

            dlgAlert.create().show();




            return false;
        }
    });

}

What context should I use in the new AlertDialog.Builder(getBaseContext()) call ?

1

There are 1 answers

0
Reaz Murshed On

Simply use...

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);