How to change the displayed hue of a button below the 23 api level programmatically in android

I am trying to understand how to programmatically change the color of a drawableLeft / drawableRight button. I used drawable tint in my xml as below, which works> api level 23 but not able to change color <api level 23

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="VIEW ALL" android:layout_centerInParent="true" android:background="#00000000" android:drawableLeft="@mipmap/ic_menu_black_36dp" android:layout_centerVertical="true" android:id="@+id/view_all" android:textColor="@color/bottom_color" android:drawableTint="@color/bottom_color" /> Button prev = (Button) findViewById(R.id.prev); Drawable[] drawables =prev.getCompoundDrawables(); drawables[0].setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); prev.setCompoundDrawables(drawables[0],null,null,null); 

Decision:

  Drawable[] drawablesprev =prev.getCompoundDrawables(); //for drawableleft drawable array index 0 drawablesprev[0].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); //for drawableright drawable array index 2 drawablesprev[2].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); //for drawabletop drawable array index 1 drawablesprev[1].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 
+5
source share
2 answers

you are using PorterDuff.Mode.MULTIPLY , so you are multiplying colors. Assuming your icon is black - #000000 or as an int , it will be 0 . then 0 * GRAY (or any other color) will always give you 0 , so it's still black ...

try other PorterDuff.Mode s for example. PorterDuff.Mode.SRC_ATOP or PorterDuff.Mode.SRC_IN

your current code is likely to work with a white version of the icon that should be correctly colored using MULTIPLY

0
source

Here's a quick way to colorize a TextView or Button drawable:

  private void tintViewDrawable(TextView view) { Drawable[] drawables = view.getCompoundDrawables(); for (Drawable drawable : drawables) { if (drawable != null) { drawable.setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); } } } 
0
source

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


All Articles