Michael
Michael

Reputation: 33317

How to change ImageButton drawable color when used as background programmatically?

I have the following ImageButton:

<ImageButton
    android:id="@+id/nopeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/nope_button_states" />

Here is nope_button_states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/nope_button" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/nope_button_selected" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/nope_button_selected" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/nope_button" />
</selector>

Now I want to change the tint color of the drawable. When I use with:

android:src="@drawable/nope_button"

then I can do: nopeButton.setColorFilter() but this does not work anymore when using in background.

How do I change the tint color from the ImageButton drawings when used with background property?

Upvotes: 2

Views: 2329

Answers (1)

Dario Picco
Dario Picco

Reputation: 177

Try using:

nopeButton.getBackground().setColorFilter();

Upvotes: 1

Related Questions