Ciro González
Ciro González

Reputation: 367

onTouchListener always getting the same MotionEvent ACTION_UP

At first i always get the ACTION_DOWN mesage so i googled and i found that i have to return TRUE at the end. So i changed it and i started to get always ACTION_UP. I dont understand why.

linearLayoutDraggable.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //ConstraintLayout.LayoutParams par = (ConstraintLayout.LayoutParams)v.getLayoutParams();
                CardView.LayoutParams par = (CardView.LayoutParams)v.getLayoutParams();


                switch (event.getAction()) {

                    case MotionEvent.ACTION_UP: {
                        //par.height = 40;
                        Toast.makeText(MagMainNewActivity.this, "UP", Toast.LENGTH_SHORT).show();
//                                par.height=300;
//                                par.topMargin = (int) event.getRawY() - (v.getHeight());
//                                par.leftMargin = (int) event.getRawX() - (v.getWidth() / 2);
//                                v.setLayoutParams(par);

                        break;
                    } //inner case UP
                    case MotionEvent.ACTION_DOWN: {
                        Toast.makeText(MagMainNewActivity.this, "DOWN", Toast.LENGTH_SHORT).show();
//                                par.height = 115;
//                                //par.width = 60;
//                                v.setLayoutParams(par);
                        break;
                    } //inner case UP
                } //inner switch

                return true;
            }
        });

Upvotes: 0

Views: 484

Answers (2)

Debela Yonas
Debela Yonas

Reputation: 11

Because before finger is up finger down is first, this means MotionEvent.ACTION_DOWN is came frist. It check event action and if finger touch a screen action is MotionEvent.ACTION_DOWN and it break. Sorry for bad English

Upvotes: 1

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

The onTouchListener receive motion gestures, so (while returning true) if you receive a ACTION_DOWN, you will receive each ACTION_MOVE finalized with only ONE ACTION_UP that means the pointer (finger) was removed from the screen at that point.

A click action is represented from a ACTION_DOWN plus a ACTION_UP, while a ACTION_DOWN without a UP is a long press.

Maybe you are seeing just the UP message from the toast since both events are trigged sequentially and the second toast overrided the first one, if you add Log.v("MMNA", "DOWN") you can check both at the Logcat

Upvotes: 1

Related Questions