Search code examples
androidandroid-preferences

How to create a single preference with multiple fields?


I have a "change password" in my application setting that I want the user be able to input 3 text fields (current password, new password, repeat password) and then just click ok button to save the settings. but the problem is I couldn't find (or been able to create) any preference that can show multiple items (in here 3 EditTextPreferences).

the below code shows just a textbox when its clicked to edit.

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

    <EditTextPreference
        android:capitalize="none"
        android:password="true"
        android:defaultValue=""
        android:inputType="textCapWords"
        android:key="example_text"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_default_current_password" />

</PreferenceScreen>

i'm looing for something like:

<Group>
        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_current_password" />

        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_new_password" />

        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_repeat_password" />
</Group>

Solution

  • You need to create a custom DialogPreference. Your approach look flawed to me, you only want a single preference value to be stored in SharedPreferences.

    Create a class extending DialogPreference, return your inflated view onCreateDialogView(), and add some logic when the positive button is clicked. You manage as it was a fragment or activity class, just extending DialogPreference:

    public class CustomPreference extends DialogPreference {
    
        EditText first;
        EditText second;
        EditText third;
    
        protected View onCreateDialogView() {
            View v = LayoutInflater.from(getContext()).inflate(R.layout.three_edit_texts, null);
            first = v.findViewById(...);
            second = v.findViewById(...);
            third = v.findViewById(...);
            return v;
        }
    
    }
    

    Then in XML you just add a:

    <com.your.package.CustomPreference />