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

3

119

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

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

Gluttony answered 26/2, 2011 at 14:23 Comment(0)
C
337

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();
Commissar answered 26/2, 2011 at 14:39 Comment(8)
I know how to build a Dialog, but my q about preferences.Gluttony
Your question is about preferences? Okay. Take a look here: kaloer.com/android-preferencesCommissar
I've already read this source. I may create custom Preference subscribed to OnClick and handle it, but is it the easiest way?Gluttony
I think so, at least I don't know any other way that is easier.Commissar
I don't know too, so I'm asking)Gluttony
The browser source itself uses onclicklistenersCommissar
Found an elegant answer in a similar post : Yes or No confirmation in Android PreferencesSalts
use import android.support.v7.app.AlertDialog; for a better look without a themePyo
N
9

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

Nephro answered 4/1, 2013 at 7:58 Comment(1)
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?!Salts
B
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.

Buie answered 12/9, 2012 at 22:37 Comment(2)
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.Lindeman
Love it. IMHO the verbose, but approved and correct wayKomsa

© 2022 - 2024 — McMap. All rights reserved.