Change TintList to StateListDrawable inside RippleDrawable

Region

RippleDrawable has selector

both item

inside. He works.

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/pink_highlight">        
    <item
        android:id="@android:id/mask"
        android:drawable="@color/pink_highlight" />
    <item 
        android:drawable="@drawable/bg_selectable_item" />
</ripple>

      

+

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/pink_highlight_focus" android:state_focused="true" />
    <item android:drawable="@color/pink_highlight_press" android:state_pressed="true" />
    <item android:drawable="@color/pink_highlight_press" android:state_activated="true" />
    <item android:drawable="@color/pink_highlight_press" android:state_checked="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

      

Problem

Can't change default selector state color with DrawableCompat.setTintList

RippleDrawable bg = (RippleDrawable) 
ResourcesCompat.getDrawable(context.getResources(), R.drawable.bg_navigation_item, null);
StateListDrawable bgWrap = (StateListDrawable) DrawableCompat.wrap(bg.getDrawable(1));
DrawableCompat.setTintList(bgWrap, new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.WHITE}));
//
someView.setBackground(bg);

      

It doesn't change the default selection state, everything else is fine.

Decision

The problem arose from - misunderstanding of how toning should work; - ColorStateList

it's better to boot completely;

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/pink_highlight">
    <item>
        <shape
            android:shape="rectangle"
            android:tint="@color/selectable_transparent_item" />
    </item>
    <item
        android:id="@android:id/mask"
        android:drawable="@color/pink_highlight" />
</ripple>

      

+

LayerDrawable bg = (LayerDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.bg_selectable_item, null);
Drawable bgWrap = DrawableCompat.wrap(bg.getDrawable(0));
DrawableCompat.setTintList(bgWrap, context.getResources().getColorStateList(R.color.selectable_white_item));
someView.setBackground(bg);

      

+3


source to share


2 answers


Have you tried it someView.setBackground(bgWrap)

? You are setting the tint bgWrap

, but you also need to set it as the background, not the old background.

EDIT: Are you sure what is setTintList

bugging? The following code works for me on all APIs> = 15 with AppCompat:



public static void tintWidget(View view, ColorStateList colorStateList) {
    final Drawable originalDrawable = view.getBackground();
    final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
    DrawableCompat.setTintList(wrappedDrawable, colorStateList);
    view.setBackground(wrappedDrawable);
}

      

+1


source


Issue 172067: DrawableCompat # setTintList doesn't work on Lollipop and above



https://code.google.com/p/android/issues/detail?id=172067

+1


source







All Articles