Search code examples
androidonclickontouch

OnClick / OnTouch user changing their mind


I am currently experimenting with onclick listeners and on on touch listeners.

I found out that using a onclick the app will always execute the action even if the user drags his/her finger off of the element. but i wanted the user to be able to change his mind and drag his/her finger off of the element so that it won't execute the action. i managed to do this with on touch.

on touch code :

action_bar_group_info.setOnTouchListener(new View.OnTouchListener() {
        int [] location = new int[4];
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            location[0] = action_bar_group_info.getLeft();
            location[1] = action_bar_group_info.getTop();
            location[2] = action_bar_group_info.getBottom();
            location[3] = action_bar_group_info.getRight();

            Log.v("Location: ", "top: "+String.valueOf(location[1]) + " left: "+String.valueOf(location[0]) + " right: "+String.valueOf(location[3]) + " bottom: "+String.valueOf(location[2]));
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if ((int) event.getX() > location[0] && (int) event.getX() < location[3] && (int) event.getY() > location[1] && (int) event.getY() < location[2]) {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
                    Intent intent = new Intent(mContext, GroupInformation.class);
                    intent.putExtra("gid", gid);
                    intent.putExtra("gname", gname);
                    intent.putExtra("image", img);
                    intent.putExtra("uid", uid);
                    startActivity(intent);
                    finish();
                }else {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
                }
            }
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                Log.v("Location pointer: x:"+ String.valueOf((int) event.getX())," Y: "+ String.valueOf((int) event.getY()));
                if ((int) event.getX() < location[0] || (int) event.getX() > location[3] || (int) event.getY() < location[1] || (int) event.getY() > location[2]) {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
                }
            }

            return false;
        }
    });

But is this really the way to do it isn't there a easier way to achieve what i want?

action_bar_group_info is a linear layout.


Solution

  • I found out that using a onclick the app will always execute the action even if the user drags his/her finger off of the element.

    No, this is not true. Draging finger out of i.e. button area won't fire OnClickListener