Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTouch, onLongClick together in android

I'm adding ImageViews to parent layout dynamically and performing zoom in/out operations with onTouch on the added image.

I want to remove the added view with an onLongPress of it.

img.setOnLongClickListener(longClickAction);
img.setOnTouchListener(touchAction); 
 

onLongPress:

OnLongClickListener longClickAction = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        parentLayout.removeView((ImageView)v);
        return false;
    }
};

onTouch:

OnTouchListener touchAction = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView)v;

        // Perform zoom operation onTouch of ImageView
        zoom(i, event);
        return true; 
    }
};

Why do only the onTouch events work?

How can I get them both to work?

What should I do to remove the added view?

like image 339
SANDHYA Avatar asked Jun 08 '12 10:06

SANDHYA


3 Answers

onTouch is always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still calls onTouch first and since you return true in onTouch(which means that you've consumed this event and it should not be further dispatched) you won't get onLongPress called. What will do the trick is returning false in onTouch

like image 97
asenovm Avatar answered Oct 19 '22 02:10

asenovm


As discussed by @asenovm The onTouch() is always called as it is the initial state of dispatching the events to the view, but if we return value false in onTouch() Then both will work like a charm and the problem will be solved.

Edit: My suggestion to the users is that instead of implementing OnLongClickListener() and OnTouch() together, try using the function of OnLongClickListener() in Double click event.

You can implement Double Click in following ways:

int i = 0;
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    i++;
    Handler handler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            i = 0;
        }
    };

    if (i == 1) {
        //Single click
        handler.postDelayed(r, 250);
    } else if (i == 2) {
        //Double click
        i = 0;
        ShowDailog();
    }


  }
});
like image 40
Pankaj Lilan Avatar answered Oct 19 '22 04:10

Pankaj Lilan


Try this, it works fine for me.

boolean isMoving= false;

yourView.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            isMoving = true;
            Log.i("isMoving:", "true");
        }
        if(event.getAction()==MotionEvent.ACTION_UP){
            isMoving=false;
            Log.i("isMoving:","false");
        }
        return false;
    }
});
yourView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if(!isMoving){
            //do onlongclick event here
        }
        return true;
    }
});;
like image 1
JasonChiu Avatar answered Oct 19 '22 03:10

JasonChiu