I am creating a settings menu for a free version of my app. I have a ListPreference displaying many different options. However, only some of these options are to be made available in the free version (I would like all options to be visible - but disabled, so the user knows what they are missing!).
I'm struggling to disable certain rows of my ListPreference. Does anybody know how this can be achieved?
findPreference("yourpref"). setEnabled(false);
A Preference that displays a list of entries as a dialog. This preference saves a string value. This string will be the value from the setEntryValues array.
Solved it.
I made a custom class extending ListPreference. I then used a custom ArrayAdapter and used methods areAllItemsEnabled() and isEnabled(int position).
public class CustomListPreference extends ListPreference {
    public CustomListPreference (Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    protected void onPrepareDialogBuilder(Builder builder) {
        ListAdapter listAdapter = new CustomArrayAdapter(getContext(), R.layout.listitem, getEntries(), resourceIds, index);
        builder.setAdapter(listAdapter, this);
        super.onPrepareDialogBuilder(builder);
    }
}
and
public class CustomArrayAdapter extends ArrayAdapter<CharSequence> {
public CustomArrayAdapter(Context context, int textViewResourceId,
        CharSequence[] objects, int[] ids, int i) {
    super(context, textViewResourceId, objects);
}
   public boolean areAllItemsEnabled() {
        return false;
    }
    public boolean isEnabled(int position) {
        if(position >= 2)
            return false;
        else
            return true;
    }
public View getView(int position, View convertView, ViewGroup parent) {
             ...
    return row;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With