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

sh1ng picture sh1ng · Feb 26, 2011 · Viewed 114.2k times · Source

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

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

Answer

Maaalte picture Maaalte · Feb 26, 2011

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();