I have a preferences screen with on checkbox header. In response to it being checked other preferences in that screen are enabled or disabled.
One of these preferences should also change it's icon and text as well as the disabled state. I can change the state, but the icon and text won't update.
xml layout for the custom preference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize" >
<ImageView
android:id="@+id/icon_wearable"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="5dp"
android:src="@drawable/wear_notifications_enabled" />
<TextView
android:id="@+id/txt_wearable_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7"
android:text="@string/notifications_enabled_wearable_preferences_title"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/actionbar_info" />
</LinearLayout>
There are image and text resources for the disabled state.
The Preference class (comments in the code show the evolution and of my attempts to get it to work):
public class WearablePreference extends Preference {
private TextView txt;
private ImageView img;
public WearablePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.wearable_notification_layout);
// setWidgetLayoutResource(R.layout.wearable_notification_layout);
}
public WearablePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.wearable_notification_layout);
// setWidgetLayoutResource(R.layout.wearable_notification_layout);
}
public WearablePreference(Context context) {
super(context);
setLayoutResource(R.layout.wearable_notification_layout);
// setWidgetLayoutResource(R.layout.wearable_notification_layout);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (txt == null && img == null) {
txt = (TextView) view.findViewById(R.id.txt_wearable_title);
img = (ImageView) view.findViewById(R.id.icon_wearable);
handleStateChange();
}
}
public void handleStateChange() {
final boolean enabled = super.isEnabled();
if (txt != null && img != null) {
txt.post(new Runnable() {//also tried it not as a separate runnable since the click event is caught and handled in the main thread anyway, but still worth the try...
public void run() {
int imgResource = enabled ? R.drawable.wear_notifications_enabled
: R.drawable.wear_notifications_disabled;
int txtResource = enabled ? R.string.notifications_enabled_wearable_preferences_title
: R.string.notifications_disabled_wearable_preferences_title;
txt.setText(txtResource);
img.setImageResource(imgResource);
notifyChanged();
}
});
}
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
// handleStateChange();
}
}
and finally the code that should have made this happen:
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.notification_preferences);
wearableNotificationPreferences = (WearablePreference) findPreference(PREFERENCE_WEARABLE);
private void toggleAllItems(boolean enabled)
{
if (wearableNotificationPreferences!=null) {
wearableNotificationPreferences.setEnabled(enabled);
wearableNotificationPreferences.handleStateChange();
}
}
In the last bit of code, the enabled state change happens, but that's it. The icon and text won't change. I can't use preferences change listeners, as there is no preference change on my particular item, though I'm considering adding a stub preference just so I can get it to work.
Does anyone know why the text and icon won't update and/or what I can do to get them to change?
Thanks, Omer.
0 comments:
Post a Comment