2

I have plenty of buttons that when pressed, should change the background color and when released, should revert to the original color (transparent).
The goal is to do this by creating as few files as possible and avoiding the need of manual handling of touch events.

Simply using a StateListDrawable is not a nice way, because it replaces the image with another one, instead of just adding a background to the image. In such a case there should be two images per button: one for up and one for down state of the button. Currently I have only for the up state, and want to remain like that.

Another way would be to use a Layer list, and then again for each button there should be a separate xml for the down state, that will combine the background color and the 'foreground' image.

Is there another way, some generic approach, that I am missing?

dragi
  • 3,385
  • 5
  • 22
  • 40
  • Does [`FancyButton`](https://github.com/medyo/Fancybuttons) cover what you're looking for? – Adil Soomro Jan 20 '17 at 15:41
  • @helleye can you explain a bit? lets say you have 10 buttons do you want to change color of all when once a button pressed or do you want to use apply that same effect for a button when its pressed without coding for all buttons? – Charuක Jan 26 '17 at 10:28
  • @Charuka The latter, I want to apply the same effect to more than one button. – dragi Jan 26 '17 at 10:40
  • @helleye why don't you go for a drawble selector xml and set it as background of you buttons? because you want to display same change for all buttons at the time of a single button click? trying to clear things before give an answer – Charuක Jan 26 '17 at 10:48
  • @Charuka When I press a button, I want to change it's background to a color, and when I release the button it should return to original background. I want to apply such behaviour to many buttons. But if I press one button, only its background should change. However, what you suggest is the same as what Yoni Gross already gave as an answer. – dragi Jan 26 '17 at 10:54
  • @helleye yes its the very simple thing that you can do. You don't need to code for touch events you can change the state when its pressed selected or for the normal state and if you are expecting such a thing dunno why you offered a bounty http://stackoverflow.com/questions/14023886/android-button-selector If you are expecting something else mention that so i can help – Charuක Jan 26 '17 at 10:57
  • @Charuka well, my buttons are actually ImageView's so I was testing by just changing the src, and I did not even consider using selector xml for a background. That is what I have been missing to realize, and that's why I offered a bounty to someone that will help me see what I miss. – dragi Jan 26 '17 at 11:24
  • @helleye wait you can even use src if you want if they are images! do you want me to give an answer in that way do you have your images in the same activity or different ones – Charuක Jan 26 '17 at 11:34
  • @Charuka what do you mean about having the images in the same activity? Or better, post your answer, it may be helpful (to me, or others) – dragi Jan 26 '17 at 12:10
  • i was asking that wether your image views are in a single activity that you want to change the src when its selected or all over the classes – Charuක Jan 26 '17 at 13:12
  • The ImageViews are in two activities and in multiple fragments.But I was changing src, that is a png, with a selector xml, that contains the same png for normal state, and a color for pressed state. – dragi Jan 26 '17 at 14:03

3 Answers3

0

Android Buttons support custom background with states, no code is needed.

The example shows using images files, but you can define shapes as well.

Yoni Gross
  • 816
  • 8
  • 16
  • Anyone care to explain why my suggestion to use the already built-in exactly for that purpose Android mechanism voted down? – Yoni Gross Jan 24 '17 at 07:58
  • So I had missed to notice that I can use a selector for a background as well. Thanks for that. But I stumbled upon the issue that there is really one instance of the selector used everywhere. So when you press a button, all buttons that use the same background drawable xml change their background color :( http://stackoverflow.com/questions/10889415/adding-a-color-filter-to-a-drawable-changes-all-buttons-using-the-same-drawable – dragi Jan 25 '17 at 14:45
  • While documentation says all drawable for the same resource share state, I guess it's not applied for `StateListDrawable`, or OS is using Drawable.mutate() or some other workaround when used in XML. I just tried, it works fine. – Yoni Gross Jan 26 '17 at 10:31
  • It didn't work fine for me, but at least works as documented. I tested on API 17. I guess I can accept the need to mutate it for each button I use. – dragi Jan 26 '17 at 11:21
  • That's weird. I've compiled with SDK 17 and ran it on API 17 emulator, yet setting the selector in XML behaves as expected. – Yoni Gross Jan 26 '17 at 15:41
0

You can use ColorFilter. like this:

public class MyButton extends Button{
    int releasedColor;
    int pressedColor;
    public MyButton(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP){
            getBackground().setColorFilter(releasedColor, PorterDuff.Mode.SRC_IN);
        }
        else if (event.getAction() == MotionEvent.ACTION_DOWN){
            getBackground().setColorFilter(pressedColor, PorterDuff.Mode.SRC_IN);
        }
        return super.onTouchEvent(event);
    }
}

You can change mode parameter for getting the best result for your case.

Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
0
...
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
v.setBackgroundColor(Color.WHITE);
 break;
...
Malik Abu Qaoud
  • 208
  • 1
  • 9