3

I need to implement onItemLongPressListener as well as onTouchListener.They don't work together so I have to return false from ontouch listner in other for item long press listener to get triggered as well.

I need the image button to change when I touch the imageButton but since my on touch listener is returning false the image stays on the pressed down state.

How can I make it change the button image when I am no longer touching the button?

imageView.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        imageView.setImageResource(mThumbIdsPressed[position]);
                    } else {
                        imageView.setImageResource(mThumbIds[position]);
                    }
                    recordAudio.startPlaying(position);

                    return false;
                }
            });
124697
  • 22,097
  • 68
  • 188
  • 315
  • I'm confused by your question, do you simply want to change the image when `event.getAction() == MotionEvent.ACTION_UP`? – Sam Mar 09 '13 at 17:58
  • 1
    @sam yes i want the image to change when the user is no longer touching the button. but since i am forced to return "false" on the ontouch, it doesnt know that the button is released – 124697 Mar 09 '13 at 18:00
  • For changing image use selector as here: http://stackoverflow.com/questions/4185930/how-to-highlight-imageview-when-focused-or-clicked . Fire your method recordAudio.startPlaying(position) you can in OnClickListener. – nfirex Mar 09 '13 at 18:01

2 Answers2

3

You can use a GestureDetector, like SimpleOnGestureListener, to make the distinction between:

Sam
  • 86,580
  • 20
  • 181
  • 179
2

I just finished doing this for my project, partly based off of you existing code (thanks for that by the way). The way I did it was instead of the "else" I did an "else if" as shown below.

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    if(event.getAction() == MotionEvent.ACTION_UP){
        touching = false;
    }else if(event.getAction() == MotionEvent.ACTION_DOWN){
        touching = true;
    }

    touch_x = event.getX();
    touch_y = event.getY();

    return true;
}

The touching boolean is used further on in the rendering thread.

Colin
  • 491
  • 1
  • 5
  • 13