How to implement a confirmation (yes/no) DialogPreference?

124,358

Solution 1

That is a simple alert dialog, Federico gave you a site where you can look things up.

Here is a short example of how an alert dialog can be built.

new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to whatever?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {
        Toast.makeText(MainActivity.this, "Yaay", Toast.LENGTH_SHORT).show();
    }})
 .setNegativeButton(android.R.string.no, null).show();

Solution 2

Android comes with a built-in YesNoPreference class that does exactly what you want (a confirm dialog with yes and no options). See the official source code here.

Unfortunately, it is in the com.android.internal.preference package, which means it is a part of Android's private APIs and you cannot access it from your application (private API classes are subject to change without notice, hence the reason why Google does not let you access them).

Solution: just re-create the class in your application's package by copy/pasting the official source code from the link I provided. I've tried this, and it works fine (there's no reason why it shouldn't).

You can then add it to your preferences.xml like any other Preference. Example:

<com.example.myapp.YesNoPreference
    android:dialogMessage="Are you sure you want to revert all settings to their default values?"
    android:key="com.example.myapp.pref_reset_settings_key"
    android:summary="Revert all settings to their default values."
    android:title="Reset Settings" />

Which looks like this:

screenshot

Solution 3

Use Intent Preference if you are using preference xml screen or you if you are using you custom screen then the code would be like below

intentClearCookies = getPreferenceManager().createPreferenceScreen(this);
    Intent clearcookies = new Intent(PopupPostPref.this, ClearCookies.class);

    intentClearCookies.setIntent(clearcookies);
    intentClearCookies.setTitle(R.string.ClearCookies);
    intentClearCookies.setEnabled(true);
    launchPrefCat.addPreference(intentClearCookies);

And then Create Activity Class somewhat like below, As different people as different approach you can use any approach you like this is just an example.

public class ClearCookies extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    showDialog();
}

/**
 * @throws NotFoundException
 */
private void showDialog() throws NotFoundException {
    new AlertDialog.Builder(this)
            .setTitle(getResources().getString(R.string.ClearCookies))
            .setMessage(
                    getResources().getString(R.string.ClearCookieQuestion))
            .setIcon(
                    getResources().getDrawable(
                            android.R.drawable.ic_dialog_alert))
            .setPositiveButton(
                    getResources().getString(R.string.PostiveYesButton),
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            //Do Something Here

                        }
                    })
            .setNegativeButton(
                    getResources().getString(R.string.NegativeNoButton),
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            //Do Something Here
                        }
                    }).show();
}}

As told before there are number of ways doing this. this is one of the way you can do your task, please accept the answer if you feel that you have got it what you wanted.

Share:
124,358

Related videos on Youtube

sh1ng
Author by

sh1ng

Updated on April 17, 2020

Comments

  • sh1ng
    sh1ng about 4 years

    How can I implement a Preference that displays a simple yes/no confirmation dialog?

    For an example, see Browser->Setting->Clear Cache.

  • sh1ng
    sh1ng over 13 years
    I know how to build a Dialog, but my q about preferences.
  • Maaalte
    Maaalte over 13 years
    Your question is about preferences? Okay. Take a look here: kaloer.com/android-preferences
  • sh1ng
    sh1ng over 13 years
    I've already read this source. I may create custom Preference subscribed to OnClick and handle it, but is it the easiest way?
  • Maaalte
    Maaalte over 13 years
    I think so, at least I don't know any other way that is easier.
  • sh1ng
    sh1ng over 13 years
    I don't know too, so I'm asking)
  • Maaalte
    Maaalte over 13 years
    The browser source itself uses onclicklisteners
  • Ralph Ritoch
    Ralph Ritoch over 10 years
    Thanks! This is almost exactly what I needed for my app since I'm triggering the dialog from a service and in my case I need an activity to host the dialog.
  • reubenjohn
    reubenjohn about 10 years
    Great solution! But there is a problem with one of the constructors of this class! It references a resource attr (com.android.internal.R.attr.yesNoPreferenceStyle)! Which cannot be resolved. What is the workaround? Do I have to create one in my own project?!
  • reubenjohn
    reubenjohn about 10 years
    Found an elegant answer in a similar post : Yes or No confirmation in Android Preferences
  • John
    John over 9 years
    Love it. IMHO the verbose, but approved and correct way
  • majurageerthan
    majurageerthan about 5 years
    use import android.support.v7.app.AlertDialog; for a better look without a theme