Mostafa Lavaei
Mostafa Lavaei

Reputation: 2039

Background Tint

How can i use background tint for ImageButton or AppCompatImageButton? Please answer for both XML and Java. I have find a java solution that says i most use setBackgroundTintList(), but when i use it, the background always shown blue and don`t change on click. This is what i have tried:

mButtonBold.setSupportBackgroundTintList(new ColorStateList(
            new int[][]{EMPTY_STATE_SET, PRESSED_ENABLED_STATE_SET},
            new int[]{Color.BLUE, Color.GREEN}
))

Upvotes: 6

Views: 27136

Answers (4)

avez raj
avez raj

Reputation: 2145

final  AppCompatImageButton imageButton = (AppCompatImageButton)findViewById(R.id.imageButton);
        imageButton.setSupportBackgroundTintList(new ColorStateList(
                new int[][]{new int[]{android.R.attr.state_selected},
                        new int[]{}
                },
                new int[]{Color.GREEN,Color.BLUE}
        ));
        imageButton.setSupportBackgroundTintMode(PorterDuff.Mode.SRC_OVER);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageButton.setSelected(!imageButton.isSelected());
            }
        });

this will toggle its state if you want like this, when selected it will be green and unselected will be blue.

Upvotes: 0

user5703518
user5703518

Reputation:

First your order of array is wrong. default value must be the last state so use this one:

mButtonBold.setSupportBackgroundTintList(new ColorStateList(
            new int[][]{PRESSED_ENABLED_STATE_SET,EMPTY_STATE_SET},
            new int[]{Color.GREEN,Color.BLUE}
))

XML

<android.support.v7.widget.AppCompatImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_done"
    app:backgroundTint="@drawable/background"
    app:backgroundTintMode="src_over"/>

drawable/background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/colorAccent" android:state_pressed="true" />

    <item android:color="@color/colorPrimary" />
</selector>

JAVA

if considering the layout is something like this:

   <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_done" />

then:

 AppCompatImageButton imageButton = (AppCompatImageButton)findViewById(R.id.imageButton);
    imageButton.setSupportBackgroundTintList(new ColorStateList(
            new int[][]{new int[]{android.R.attr.state_pressed},
                        new int[]{}
            },
            new int[]{Color.GREEN,Color.BLUE}
    ));
    imageButton.setSupportBackgroundTintMode(PorterDuff.Mode.SRC_OVER);

if you are going to use SDK Widget use android prefix instead of app where

xmlns:app="http://schemas.android.com/apk/res-auto"

and

xmlns:android="http://schemas.android.com/apk/res/android"

Upvotes: 14

Rubber
Rubber

Reputation: 66

To set the background tint of a button :

i have a tip with xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" android:color="#ffff0000"/> 
<!-- pressed --> 
<item android:state_focused="true" android:color="#ff0000ff"/> 
<!-- focused --> 
<item android:color="#ff000000"/> 
<!-- default --> 
</selector>

Then set the background to button with:

button.setBackgroundResource(R.drawable.yourbackgroundbutton);

Upvotes: 0

mhdjazmati
mhdjazmati

Reputation: 4232

Changing Tint in JAVA Code :

imageView.setColorFilter(Color.argb(255, 255, 0, 0)); // RED Tint

Changing Tint in XML Code :

android:tint="@color/red"

Upvotes: 1

Related Questions