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..?
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.
setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.
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.
setIcon() method is use to set the icon on Alert dialog box.
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();
}
}
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!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With