Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling rows in ListPreference

Tags:

android

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?

like image 512
Adam Smith Avatar asked Mar 10 '12 02:03

Adam Smith


People also ask

How do I turn off preferences on Android?

findPreference("yourpref"). setEnabled(false);

What is list preference in android?

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.


1 Answers

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;
}
like image 57
Adam Smith Avatar answered Oct 11 '22 20:10

Adam Smith