Custom layout ListPreference

10,378

Solution 1

You cannot create a custom layout for a ListPreference. You can, however, create your own custom DialogPreference and set that up to look like whatever you wish.

For example, here is a DialogPreference that uses a TimePicker to allow the user to choose a time. Here is a DialogPreference that allows the user to choose a color.

Solution 2

in your preference.xml file you can refer to your custom ListPreference by the full name of the class i.e. com.example.MyPreference

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="pref_wifi_key"
    android:title="@string/settings">
    <ListPreference
        android:key="pref_wifi_remove"
        android:title="@string/remove_wifi"/>
    <com.example.MyPreference
    android:title="@string/add_wifi"/>
</PreferenceScreen>

Then your class MyPreference hould be something like this:

import android.preference.ListPreference;

public class MyPreference extends ListPreference  {

    Context context;

    public MyPreference(Context context, AttributeSet attrs) {
        this.context = context;
        setDialogLayoutResource(R.layout.yourLayout); //inherited from DialogPreference
        setEntries(new CharSequence[] {"one", "two"});
        setEntryValues(new CharSequence[] {"item1", "item2"});
    }
    protected void onDialogClosed(boolean positiveResult) {
    Toast.makeText(context, "item_selected",
                       Toast.LENGTH_SHORT).show();
    }
}

Solution 3

  1. in your preference xml file

    <your.domain.CustomListPreference .../>

  2. CustomListPreference.java

    class CustomListPreference extends ListPreference {
    
    mListAdapter = new your_custom_list_adapter();
    private int mClickedDialogEntryIndex;
    public CustomListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomListPreference(Context context) {
        super(context);
    }
    
    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
          mClickedDialogEntryIndex = findIndexOfValue(getValue());
          builder.setSingleChoiceItems(mListAdapter, mClickedDialogEntryIndex,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (mClickedDialogEntryIndex != which) {
                            mClickedDialogEntryIndex = which;
                            CustomListPreference.this.notifyChanged();
                        }
                        CustomListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();
                    }
                });
          builder.setPositiveButton(null, null);
    }
    
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        CharSequence[] entryValues = getEntryValues();
        if (positiveResult && mClickedDialogEntryIndex >= 0 && entryValues != null) {
            String value = entryValues[mClickedDialogEntryIndex].toString();
            if (callChangeListener(value)) {
                setValue(value);
            }
        }
    }
    
    }
    
Share:
10,378
dozhdik
Author by

dozhdik

Updated on June 29, 2022

Comments

  • dozhdik
    dozhdik almost 2 years

    Prompt please where it is possible to read about creating a custom layout listpreference ( background and layout top panel, panel button ). Met - examples only for custom row. Sorry - google translate.