Preference with Confirm Dialog
Asked Answered
N

3

6

I am having a hard time making a Preference with Dialog, none of the methods suggested in the site works for me. So here is what I have first. In MainActivity I have this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_stage_1, menu);
    return true;
}

This opens the menu, and when you choose Settings, in the next screen there is only one preference, created from this preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<Preference
    android:summary="clears all data"
    android:title="Reset Data" >
    <intent
        android:targetClass="com.example.myapp.settings.DialogActivity"
        android:targetPackage="com.example.myapp" >
    </intent>
</Preference>

I want when I click this option, a dialog to appear with Yes and No button, so I ca define somewhere what the buttons will do programatically. How can I do it?

Newbold answered 18/8, 2014 at 11:24 Comment(0)
P
6

You can extend the DialogPreference. Override the onDialogClosed method, which is called when the Dialog closes. The argument to the method indicates whether user selected positive or negative button.

CustomDialogPreference.java :

    public class CustomDialogPreference extends DialogPreference {
        public CustomDialogPreference(Context context, AttributeSet attrs) {
            super(context, attrs);

            // Set the layout here                
            setDialogLayoutResource(R.layout.custom_dialog);

            setPositiveButtonText(android.R.string.ok);
            setNegativeButtonText(android.R.string.cancel);

            setDialogIcon(null);
        }

        @Override
        protected void onDialogClosed(boolean positiveResult) {
            // When the user selects "OK", persist the new value
            if (positiveResult) {
                // User selected OK
            } else {
                // User selected Cancel
            }
        }

    }

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.example.test1.CustomDialogPreference
        android:key="pref_dialog"
        android:title="dialog"/>
</PreferenceScreen>

You can refer the official Settings documentation.

Peralta answered 21/8, 2014 at 14:21 Comment(1)
I'd like to add that you don't necessarily have to override the layout. If you just want to display a custom message with the default confirmation layout, you can use the setDialogMessage() method inside the constructor with a resource ID to a string value.Lidialidice
E
1

In this case, if you can using Android Studio, you can right click the folder that contains your activities and choose to create a new SettingsActivity.

Then in your xml for ActionBar in MainActivity, set up the setting button like this:

<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="99"
    android:showAsAction="ifRoom"/>

Then set up Listener for the Setting button on ActionBar:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        //Start your new Activity here
        return true;
    }
    return super.onOptionsItemSelected(item);
}

You can still use my code below on building a AlertDialog in your Listener in SettingsActivity.

AlertDialog.Builder alert = new AlertDialog.Builder(self);
alert.setTitle(getResources().getString(R.string.action_about));
//Set up your AlertDialog and buttons
alert.setMessage(message);
alert.setNegativeButton("Okay", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
    }
});
alert.setCancelable(true);
alert.show();
Eichelberger answered 18/8, 2014 at 11:57 Comment(4)
where do I place this method, how do I start the class that has this method, please explain moreNewbold
I actually want this dialog to show in the PreferenceActivity, like when I select Settings from the menu in MainActivity, then the preferenceactivity opens up, and the first option would be Clear Data, so I want there to be shown a Dialog with confirmationNewbold
Okay. In this case, if you can using Android Studio, you can right click the folder that contains your activities and choose to create a new SettingsActivity. You can still use my code on building a AlertDialog in your Listener in SettingsActivity.Eichelberger
And I have added some additional codes in my answer.Eichelberger
P
0

If you use v7 support library its a bit complicated... But I prefer following:

Firstly create Preference

<PreferenceCategory
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:title="@string/settings_absence_category_data">

        <Preference
            android:key="absence_data"
            android:title="@string/settings_absence_data_clear"></Preference>
    </PreferenceCategory>

then bind on fragment prefererence

 Preference clear = findPreference("absence_data");

        clear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                alertForClearAbsenceData();

                return true;
            }
        });

alertForClearAbsenceData function below for alertdialog

 private void alertForClearAbsenceData(){

        AlertDialog.Builder alert = new AlertDialog.Builder(getMainActivity());

        alert.setTitle(getString(R.string.fragment_absence_alert_title));
        alert.setMessage(getString(R.string.fragment_absence_alert_message));

        alert.setCancelable(false);
        alert.setPositiveButton(R.string.fragment_absence_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });
        alert.show();
    }

This is example how to confirm alert dialog

Phila answered 3/5, 2018 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.