Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch listener not working android

Tags:

android

I have a problem with touch listener in my android application. OnTouchLIstenr is not working for the view, i.e ACTION_DOWN is performing well in the listener but ACTION_UP doesnt invokes. i dont know whats the problem going on. But if i set the dummy click listener, both working fine. Why is it so?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView image = (ImageView) findViewById(R.id.image);
    image.setOnTouchListener(new OnTouchListener() {            
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView img = (ImageView) v;
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN){
                img.setImageResource(R.drawable.port);
            }else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){
                img.setImageResource(R.drawable.bar);
            }               
            return false;
        }
    });

}
like image 376
fargath Avatar asked May 26 '11 05:05

fargath


2 Answers

You might consider, returning 'true', since you are handling the touch event.

Link to a similar question. Answer from adamp, makes sense

like image 60
varuaa Avatar answered Oct 10 '22 02:10

varuaa


image.setOnTouchListener(new OnTouchListener() {            
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView img = (ImageView) v;
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN){
                img.setImageResource(R.drawable.port);
            }else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){
                img.setImageResource(R.drawable.bar);
            }               


       return true;
        }
    });

just replace return true; instead of return false; and check

like image 45
Niranj Patel Avatar answered Oct 10 '22 01:10

Niranj Patel