Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Set alpha on button on click. Preferably xml only

I want to introduce a 'down' style on some buttons. I've created a style, and a state list.

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

    <item android:drawable="@drawable/inactive_button_background" android:state_enabled="false"/>
    <item android:drawable="@drawable/active_button_background" android:state_enabled="true"/>
    <item android:drawable="@drawable/pressed_button_background" android:state_pressed="true"></item>
</selector>

The problem is that I want to be able to change just the alpha of the background when the button gets clicked (I will have variable backgrounds, so setting a sold color with alpha channel is not a solution).

And I want to do this from the declarative xml only (don't want to polute my code with layout stuff).

Problem is I don't know how to apply this alpha blending to the button from a xml drawable. I am pretty sure there's a way though.

like image 557
George Avatar asked Apr 10 '13 07:04

George


People also ask

How do you change opacity in xml?

Create a drawable xml resource with bitmap as the root say bg. set android:src="@drawable/background" to the image which you want as the background. set android:alpha="0.4" to any value between 0 to 1 as per the required opacity.

What is Alpha attribute in android?

"alpha" is used to specify the opacity for an image.

How do I create a button style in android?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.

What is selector in android xml?

A selector can be an xml file created inside the drawable folder. Selector for different background colors. The following selector file btn_bg_selector. xml contains the code for setting different background colors on a button for different states.


3 Answers

I am bit late to answer this question but i just did this by changing the alpha value using View.onTouchListner

You can do this by implementing onTouch in this way

 @Override
public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            v.setAlpha(0.5f);
            break;
        case MotionEvent.ACTION_UP:
            v.setAlpha(1f);
        default : 
         v.setAlpha(1f)
    }
    return false;
}

I guess you cant change alpha value from drawable so you will have to do this in this way only.

like image 60
Rushabh Bhatt Avatar answered Sep 19 '22 01:09

Rushabh Bhatt


This works perfectly


yourView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                v.setAlpha(0.5f);
                break;
            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                v.setAlpha(1f);
                break;
            }
        }
        return false;
    }
});
like image 45
Michael Avatar answered Sep 19 '22 01:09

Michael


Adding something to Michael answer, if you are looking for a "softer" click animation. I added this:

btn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {
            switch (motionEvent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    v.animate().alpha(0.3f).setDuration(200).start();
                    break;
                case MotionEvent.ACTION_UP:
                    v.animate().alpha(1f).setDuration(200).start();
                default :
                    v.setAlpha(1f);
            }
            return false;
        }
    });
}

with lies on the same idea but with a animation

like image 40
Diogo Rosa Avatar answered Sep 18 '22 01:09

Diogo Rosa