androidpreferencescreenandroid-settingsedittextpreference

Android preferences, customize EditTextPreference


I am setting up the PreferenceScreen for my Android application. I included a EditTextPreference to specify a remote server. Now I want to provide the user with an option to reset the address to the initial value inside the popup:

Basically what I want is a third button, "RESET" besides "CANCEL" and "OK"

enter image description here

I there any easy way to achieve this customization ?

Code for completness:

<PreferenceCategory android:title="General">

    <EditTextPreference
        android:defaultValue="http://stackoverflow.com/"
        android:dialogTitle="Specify the remote server: "
        android:inputType="textUri"
        android:key="serverAddress"
        android:maxLines="1"
        android:summary="somesSummaryText"
        android:title="Server Address"
        android:persistent="true" />

</PreferenceCategory>


Solution

  • I think there is no way of doing so using the EditTextPreference but you can always have a simple Preference and add your AlertDialog in its OnClick with whatever customization you have in mind.

    <Preference
        android:key="pref_key_edit"
        android:summary="@string/pref_summary_edit"
        android:title="@string/pref_title_edit"/>
    

    and override onPreferenceTreeClick your PreferenceActivity or PreferenceFragment

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        switch (preference.getKey()) {
            case "pref_key_edit":
                // Do whatever you want here
                return true;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }