Feroz Siddiqui Feroz Siddiqui - 3 months ago 7
Android Question

I am trying to figure out how to change color of icon which is in drawable left of button

I am trying to figure out how to change color of icon which is in drawable left of button.

below is the xml code i am using :

<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_toRightOf="@+id/student_images"
android:drawableLeft="@mipmap/ic_email_black_18dp"
android:text=" myemail@gmail.com "
android:layout_below="@+id/email"
android:background="#00000000"
android:layout_marginBottom="20dp"
android:fontFamily="sans-serif"
android:textColor="@color/gray_text_color"
/>


I have tried the android:tint but color of icon is not changing. I am stuck here. Please help me out on this.

Answer

You can set tint programmatically like below:

int tintColor = ContextCompat.getColor(context, android.R.color.darker_gray);

Button button = (Button) findViewById(R.id.button);

Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.ic_email_black_18dp);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), tintColor);

drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

button.setCompoundDrawables(drawable, null, null, null);

Or you can use library Support Drawable Tints by snodnipper.
This library enables to set tint for drawableLeft of Button.
https://github.com/snodnipper/android-appcompat-issue198613