Hugo Aboud
Hugo Aboud

Reputation: 472

Android: Change src alpha and keep background (blend src with color)

I've been looking for this for a while but can't really figure out any clean way to do it.

I'm developing an app with some buttons, mostly defined like this:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:contentDescription="@null"
    android:src="@drawable/btn1" />

There's a onTouchListener that changes the button alpha with setAlpha(0.5f), but it also changes the background alpha, which is not what I need. I'd like to make it look darker, not trasparent.

I've seen this question: Android Graphics: Changing the alpha of just an image (not the background), and the solution actually works, but it doesn't sound good for me to create a FrameLayout for every button in my app.

Given the fact that the background is a flat color I thought there would be a simpler solution such as color blending or something like that, but really couldn't find it anywhere.

SOLUTION

Just found out this post: http://tech.chitgoks.com/2011/04/17/android-change-imagebutton-tint-to-simulate-button-pressed/

The solution is as simple as:

button.setColorFilter(Color.argb(150,0,0,0));

Upvotes: 2

Views: 1861

Answers (1)

Vladimir Mironov
Vladimir Mironov

Reputation: 30874

You can get image drawable using getDrawable() method and try to change its alpha.

Upvotes: 2

Related Questions