Search code examples
androidfacebookonlongclicklistener

OnLongClickListener on image view is not working in facebook chat head implementaion


I tried to create facebook chat head service. with refernce this link . OnTouch method working fine.On long press the image view I want to delete the image view from the current view.I used OnLongClickListener(), it is not working and shows no error.How to delete the image view On long press.


Solution

  • It seems you are overriding on touch listener.

    chatHead.setOnTouchListener(new View.OnTouchListener() {
      private int initialX;
      private int initialY;
      private float initialTouchX;
      private float initialTouchY;
    
      @Override public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            initialX = params.x;
            initialY = params.y;
            initialTouchX = event.getRawX();
            initialTouchY = event.getRawY();
            return true;
          case MotionEvent.ACTION_UP:
            return true;
          case MotionEvent.ACTION_MOVE:
            params.x = initialX + (int) (event.getRawX() - initialTouchX);
            params.y = initialY + (int) (event.getRawY() - initialTouchY);
            windowManager.updateViewLayout(chatHead, params);
            return true;
        }
        return false;
      }
    });
    

    So you need to implement long click manually.

    A pseudo code for that would be something like:

     case MotionEvent.ACTION_DOWN:
            initialX = params.x;
            initialY = params.y;
            initialTouchX = event.getRawX();
            initialTouchY = event.getRawY();
            action_down_time = System.currentTimeMillis()
            return true;
     case MotionEvent.ACTION_UP:
            //long click triggered if held for at least 2 sec (2000ms)
            if (System.currentTimeMillis() - action_down_time > 2*1000) {
              your_long_click_callback_function()
            }
            return true;
    

    EDIT: You might also want to check the timings in ACTION_MOVE.