Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply color filter on button

I have a lot of button with a lot of different background colors. I want to know if there is a way to apply some color filter on click. For example, i want that all my buttons become darker on click. They keep the original color, but it's darker.

Is there an easy way to do it, or i have to define the darker color for each button?

Thanks.

like image 654
Bernie Avatar asked Jul 30 '13 12:07

Bernie


2 Answers

I assume you want the button to darken on touch, and revert to normal when the user releases the button.

I would suggest creating a custom button which does the work for you:

import android.content.Context;
import android.graphics.LightingColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

public class DarkenButton extends Button {

    public DarkenButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DarkenButton(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // darken background
            getBackground().setColorFilter(
                    new LightingColorFilter(0xff888888, 0x000000));
            break;

        case MotionEvent.ACTION_UP:
            // clear color filter
            getBackground().setColorFilter(null);
            break;
        }
        return super.onTouchEvent(event);
    }

}

Then use DarkenButton anywhere you would normally use a Button.

like image 199
Jens Zalzala Avatar answered Oct 21 '22 10:10

Jens Zalzala


Try this-

 button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            v.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));

            }


        }

and for clear filter do this

button.getBackground().clearColorFilter();
like image 21
T_V Avatar answered Oct 21 '22 09:10

T_V