2

I've added a widget layout to my preference screens showing an arrow icon, to indicate to users that there are more preferences behind a given PreferenceScreen.

However, I notice when a PreferenceScreen is not enabled it is darkened. Is there a way I could also change the widget layout being used, or make it stateful, so that I could use a faded icon when my PreferenceScreen is disabled? Similar to how a state list drawable can be applied as a button's background?

tl;dr: How can I change the icon displayed when an instance of PreferenceLayout is disabled?

THelper
  • 15,333
  • 6
  • 64
  • 104
Nathan Fig
  • 14,970
  • 9
  • 43
  • 57

2 Answers2

3

This can be done solely with XML in 3 steps.

First, create a stateful drawable that can change according to whether the widget using it is enabled, disabled, etc. So statelist_example.xml kept in the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@drawable/expander_ic_minimized"
    android:state_enabled="true" />
<item
    android:drawable="@drawable/expander_ic_minimized_faded"
    android:state_enabled="false" />
</selector>

Then define your own layout to use this drawable in the layouts folder. This will become the "widgetLayout" for the Preference. So layout_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/widget_frame"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
    android:id="@+id/icon"
    android:src="@drawable/statelist_example"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dip"
    android:layout_marginRight="6dip"
    android:layout_gravity="center" />
</LinearLayout>

Third, specify the widgetLayout in every preference to use this layout:

        <Preference
        android:key="preference_example"
        android:title="@string/preference_title"
        android:widgetLayout="@layout/layout_example" />

So basically you have your Preference reference a Layout which references a stateful drawable.

Android Docs on Statelist Drawable.

Android Docs on Preference widgetLayout.

Nathan Fig
  • 14,970
  • 9
  • 43
  • 57
1

Nice method

The best way I can see to tackle this problem is to create your own class which extends a Preference/PreferenceScreen. The layout resource you want is called in the initialisation of the class:

 setWidgetLayoutResource(R.layout.preference_color); 

Which creates the view to be displayed in the widget area, which I don't think get grayed out when the preference is disabled.

Hacky/Ugly Method

Afraid I've only come up with a hack around this issue, I don't believe the widgets support states.

Just change the widget programatically:

   Preference BGColor = findPreference("Setting_BG_Color"); 
   BGColor.setWidgetLayoutResource(R.layout.ic_custom);

Then change the widget layout when each time a dependency is called (kinda messy).

pref1 = (Preference) findPreference("Setting_Pref1");
pref1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // Check what object returned (assume checkbox pref)
            if ((boolean)newValue){
                BGColor.setWidgetLayoutResource(R.layout.ic_custom2);
                }
            return true;
        }
    }); 
stealthcopter
  • 13,964
  • 13
  • 65
  • 83
  • Ah! Sorry your post just reminded me I never posted the solution to this problem, which I've added below. Thanks for taking the time though! This might yet be helpful where my solution doesn't meet someone's needs. – Nathan Fig Jun 02 '11 at 12:56