Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open AlertDialog from preference screen?

I am developing Application in android I want to show AlertDialog if user check the checkboxpreference from preference screen. so how i can do that..?

like image 675
Dharmendra Avatar asked Mar 28 '11 09:03

Dharmendra


People also ask

How do I get view from AlertDialog?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view.

How do I use AlertDialog?

setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

Which method is used to set in AlertDialog?

setIcon() method is use to set the icon on Alert dialog box.


2 Answers

Try this one ...

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

      public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
                final Preference preference) {

         if(preference.equals("MyCheckboxPreferenceKey")) {

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

         builder.setMessage("Your Message");

         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, int id) {

             //action on dialog close
           }

         });

         builder.show();

      }


}
like image 150
Vaibhav Jani Avatar answered Oct 21 '22 20:10

Vaibhav Jani


Override onSharedPreferenceChanged in your PreferenceActivity class:

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    ...
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("MyCheckboxPreferenceKey")) {
            //Show your AlertDialog here!
        }
    }
like image 31
GrAnd Avatar answered Oct 21 '22 20:10

GrAnd