Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getStateListDrawable should set also the pressedColor on state_pressed for API < 21 #333

Closed
marbat87 opened this issue Apr 4, 2017 · 2 comments

Comments

@marbat87
Copy link

marbat87 commented Apr 4, 2017

Hi,

I was trying to set a selectableBackroundCompat on a Item, but I noticed that using that mehtod, when you click on the view no effect is shown on API < 21

I saw that, for API < 21, you're calling the following utility method inside DrawableUtils:

    public static Drawable getSelectableBackgroundCompat(@ColorInt int normalColor, @ColorInt int pressedColor, @ColorInt int rippleColor) {
        return (Drawable)(Utils.hasLollipop()?new RippleDrawable(ColorStateList.valueOf(rippleColor), getStateListDrawable(normalColor, pressedColor), getRippleMask(normalColor)):getStateListDrawable(normalColor, pressedColor));
    }

...

    private static StateListDrawable getStateListDrawable(@ColorInt int normalColor, @ColorInt int pressedColor) {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{16843518}, getColorDrawable(pressedColor));
        states.addState(new int[0], getColorDrawable(normalColor));
        if(!Utils.hasLollipop() || Utils.hasNougat()) {
            short duration = 200;
            states.setEnterFadeDuration(duration);
            states.setExitFadeDuration(duration);
        }

        return states;
    }

I saw that 16843518 corresponds to state_activated.
I think that ONLY for API < 21 you should add also the state_pressed.
You could replace the call to getStateListDrawable, with a new getStateListDrawableLegacy, like this one:

    public static Drawable getSelectableBackgroundCompat(@ColorInt int normalColor, @ColorInt int pressedColor, @ColorInt int rippleColor) {
        return (Drawable)(Utils.hasLollipop()?new RippleDrawable(ColorStateList.valueOf(rippleColor), getStateListDrawable(normalColor, pressedColor), getRippleMask(normalColor)):getStateListDrawableLegacy(normalColor, pressedColor));
    }

..

	private static StateListDrawable getStateListDrawableLegacy(@ColorInt int normalColor, @ColorInt int pressedColor) {
		StateListDrawable states = new StateListDrawable();
		states.addState(new int[]{16843518}, getColorDrawable(pressedColor));
		states.addState(new int[]{16842919}, getColorDrawable(pressedColor));
		states.addState(new int[0], getColorDrawable(normalColor));
		if(!Utils.hasLollipop() || Utils.hasNougat()) {
			short duration = 200;
			states.setEnterFadeDuration(duration);
			states.setExitFadeDuration(duration);
		}

		return states;
	}

I tested it and it works fine ob both 6.0.1 or 4.1 Android versions.

Thanks & Regards

@davideas
Copy link
Owner

davideas commented Apr 4, 2017

@marbat87, thanks for the improvement suggestion, I will check it out better, later.

@davideas
Copy link
Owner

@marbat87, what is the difference visually? I can't see it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants