Assign a color to the added image with "android:drawableLeft"

Asked

Viewed 106 times

1

I have a due case that I am using three images for each of my buttons, as shown below the images:

  • ic_palette_black_36dp
  • ic_palette_red_36dp
  • ic_palette_blue_36dp

Code:

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Button"
    android:drawableLeft="@drawable/ic_palette_black_36dp"
/>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Button"
    android:drawableLeft="@drawable/ic_palette_red_36dp"
/>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Button"
    android:drawableLeft="@drawable/ic_palette_blue_36dp"
/>

Screenshot inserir a descrição da imagem aqui

One way to do this, however in Lollipop, Android 5. + is by setting a tone in a drawable bitmap, in this way:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_back"
    android:tint="@color/red_tint"/>

I’m using Android 2.3.3 Gingerbrend and Eclipse, so I would like to find a solution without using any lib. It is possible to set a color for the added image with the android:drawableLeft in these conditions? If anyone can help, thank you!

1 answer

1

With the help of Appcompat, it is possible, yes.

Given the following provision...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pablo.tintdrawableleft.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">


        <Button
            android:id="@+id/botao_preto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_restaurant_black_24dp"
            android:drawableStart="@drawable/ic_restaurant_black_24dp"
            android:drawablePadding="8dp"
            android:text="Botão preto"/>

        <Button
            android:id="@+id/botao_vermelho"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_restaurant_black_24dp"
            android:drawableStart="@drawable/ic_restaurant_black_24dp"
            android:drawablePadding="8dp"
            android:text="Botão vermelho"/>

        <Button
            android:id="@+id/botao_azul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_restaurant_black_24dp"
            android:drawableStart="@drawable/ic_restaurant_black_24dp"
            android:drawablePadding="8dp"
            android:text="Botão azul"/>

    </LinearLayout>


</RelativeLayout>

... we can use the following function to get the result:

private void pintarDrawableLeft(Button botao, int recursoDeCor) {
    Drawable leftDrawable = DrawableCompat.wrap(botao.getCompoundDrawables()[0].mutate());
    DrawableCompat.setTint(leftDrawable, ResourcesCompat.getColor(getResources(), recursoDeCor, getTheme()));
    botao.setCompoundDrawables(leftDrawable, null, null, null);
}

What we’re doing is: first we take the drawable from the left of the button. This can be done with the function getCompoundDrawables. It returns an array of four positions. O Drawable from the left is from position 0. After that, we do mutate() in it to prevent the color change from affecting all other uses of the same image. Then we use DrawableCompat.wrap() to "package" our Drawable in another that allows the use of setTint. Finally, we put back the already colored Drawable in compoundDrawables.

Note: It is not working for some reason on API 16 (although it is working on 10 and 23). I will update as soon as I find a solution.

  • Speak, my friend, quiet! Well, I forgot to mention that I am looking for a solution for API 10 without having to insert some lib. But just below in the question it appears to work in versions smaller than Lollipop.

  • But you don’t need another library. Only Appcompat. Aren’t you using Appcompat? It is practically mandatory to use it if you want to support older versions of the system.

  • I work for a company that developed an internal app three/four years ago but I still haven’t convinced them to work with the more modern versions, but I’m working on it. So I didn’t want to add libs to make just a few changes etc. The app is not in the Playstore, so I ended up developing a service to check details of the devices being installed. Then I’ll have a static to make some kind of decision.Dude, to tell you the truth, I’ve already solved the problem differently, but I wanted someone to publish something cool so I can give the credit

  • @Cleidimarviana You’ll have to practically rewrite the Appcompat to do this. I understand your concern to add libraries to little, but that’s no small thing. Appcompat is the standard (official) compatibility library. Without it it is even difficult to support new versions without breaking old ones. It is already included when you create a project in Android Studio, inclusive. To take it out you have to know what is doing. Since the app is not in the store, this is one more reason not to have to worry about the size.

  • Anyway, whenever you ask a question like this, make it explicit that you do not want to use Appcompat, to avoid this kind of thing.

  • Blz! I just forgot to make it explicit. Thanks for the tip. Here we use Eclipse and we do not use Appcompat. Single lib more up-to-date in relation to Android developer is support-v4.jar. The other libs are for specific cases. Hugs. Vlw

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.