1

I create views dynamically, and I handle their click events normally, like this :

myView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.setBackgroundColor(0xaaffffff);


            }
        });

But this code does not handle the onRelease event, so the view remains with white background, after the click.

So how can I set the background to another value when the clicked state is over ?

Thanks in advance !

Edit:

OnTouchListener does the exact same thing. I guess I need something like onReleaseListener ?

mobilGelistirici
  • 449
  • 4
  • 7
  • 19

4 Answers4

3

For that you need to use an onTouchListener => documentation

Refer this tutorial on how to use this.

Example:

myView.setOnTouchListener(
        new View.OnTouchListener() {
        public boolean onTouch(View myView, MotionEvent event) {
            int action = event.getAction();
            if (action==MotionEvent.ACTION_MOVE)
            {
                myView.setBackgroundColor(Color.BLUE);
            }
            if (action==MotionEvent.ACTION_UP)
            {
                myView.setBackgroundColor(Color.RED);
            }
            if (action==MotionEvent.ACTION_DOWN)
            {
                myView.setBackgroundColor(Color.YELLOW);
            }
            // TODO Auto-generated method stub
            return true;
        }
    }    
Brams
  • 584
  • 4
  • 9
  • Oh, simple and beautiful. Thanks :) I will accept this. But any ideas on how to do this with transitions ? – mobilGelistirici Nov 12 '13 at 14:39
  • And one little problem, ACTION_UP does not fire, after DOWN + MOVE. Any workaround for this ? – mobilGelistirici Nov 12 '13 at 14:45
  • @mobilGelistirici try to return `true` instead of `false` in the `onTouch` function. See edited answer. Returning `true` means you are consuming the event, therefore you will get the subsequent events. – Brams Nov 12 '13 at 14:51
  • Still no success. I asked a different question about that. http://stackoverflow.com/questions/19932482/android-action-up-does-not-get-fired-after-action-move accepting your answer now. Thanks for your help ! – mobilGelistirici Nov 12 '13 at 15:01
  • Ok thanks. Good luck with it. I would suggest to return true when you are consuming the event... You are not far from the right thing to do – Brams Nov 12 '13 at 15:04
2

Android has a selector for this. Define it in a xml file in your drawable folder and use it as background:

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

    <item android:state_pressed="false" android:drawable="@drawable/YOURDRAWABLE" />           

    <item android:state_pressed="true" android:drawable="@drawable/YOURDRAWABLE_WHEN_CLICKED" />          

</selector>

Check the DOCUMENTATION

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

State lists are meant to be for style updates on state changes. Check this out. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
0

You dont need to go for such cómplicated method for capturing a "click" event. Just for this method :-

//Inside on touch listener of course :-

KOTLIN :-

    if(event.action == MotionEvent.ACTION_UP && event.action != MotionEvent.ACTION_MOVE) {
// Click has been made...
// Some code
}

JAVA :- Just replace event.action with event.getAction()

This works for me

costomato
  • 102
  • 2
  • 8