How to cancel Button using appcompat 23.3.0?

I am having a number of problems with the new AppCompat 23.3.x and drawables. First of all, I had to go back and delete:

vectorDrawables.useSupportLibrary = true 

Since AppCompat returned this now, my application generates PNG again. Well, however, I tinted the (drawableTop) button in such a way that it completely stopped working (for devices prior to M).

Here is the method I used:

 private void toggleState(boolean checked) { Drawable[] drawables = getCompoundDrawables(); Drawable wrapDrawable = DrawableCompat.wrap(drawables[1]); if (checked) { DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(), R.color.colorPrimary)); setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary)); } else { DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(), R.color.icon_light_enabled)); setTextColor(ContextCompat.getColor(getContext(), R.color.text_primary_light)); } } 

The fact is that I have a custom Button class that can be checked if it is installed, drawableTop and text have a different color if it is not checked.

This was done (with appcompat 23.2.0), but now it is not (below Android M). Believe me or not, but by doing this when it reaches setTint , the icon no longer displays.

To make it work, I have to do the following:

  DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),R.color.colorPrimary)); setCompoundDrawablesWithIntrinsicBounds(null, wrapDrawable, null, null); 

Thus, every time I tint them, I have to call setCompoundDrawablesWithIntrinsicBounds again. It does everything to work again. However, I am a little worried about tuning every time. Basically, I am wondering if there is a better way or something that I do not see.

I know that Button has setCompoundDrawableTintList , which would be wonderful, but its minimum API is level 23.

+5
source share
1 answer

Not enough reputation to make a comment, but I just ran into this problem. Really sucks and is a good thing that I caught today, and not after graduation.

 /** * Tinting drawables on Kitkat with DrawableCompat * requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it * @param view The view containing the drawable to tint * @param tintColor The color to tint it. */ public static void tintViewBackground(View view, int tintColor) { Drawable wrapDrawable = DrawableCompat.wrap(view.getBackground()); DrawableCompat.setTint(wrapDrawable, tintColor); view.setBackground(wrapDrawable); } /** * Tinting drawables on Kitkat with DrawableCompat * requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it * @param drawable The drawable to tint * @param tintColor The color to tint it. */ public static void tintDrawable(Drawable drawable, int tintColor) { Drawable wrapDrawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(wrapDrawable, tintColor); } 

Even more, this ONLY happens on Lollipop, normal behavior is observed on Kitkat and API 23+.

+5
source

Source: https://habr.com/ru/post/1247742/


All Articles