Martin Jäkel
Martin Jäkel

Reputation: 332

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>

Upvotes: 2

Views: 1384

Answers (2)

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

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

Upvotes: 2

user6363583
user6363583

Reputation: 104

OK, you can use a Preference in the Preference Screen for this. And on Preference's onPreferenceClickListener show your custom dialog and get value from the dialog and save them using SharedPreference.

Upvotes: 0

Related Questions