0

I use the onTouchEvent method to be able to have a drag&drop event and a click event. I test the result on my Nexus 7 (2012), it's working really great but on my smarphone a Asus Zenfone, it's not and I can't see why, here is my code :

button.setOnTouchListener(new View.OnTouchListener() {
    private float startX, startY;
    private long startClickTime;

    @Override
    public boolean onTouch(final View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                startClickTime = Calendar.getInstance().getTimeInMillis();
                break;

            case MotionEvent.ACTION_MOVE:
                if (v instanceof Button) {
                    Button button = ((Button) v);
                    if (button.getText().length() == 0) return true;

                    ClipData clipData = ClipData.newPlainText("", "");
                    DragShadowBuilder shadowBuilder = new DragShadowBuilder(v, false, scale);
                    v.startDrag(clipData, shadowBuilder, v, 0);

                    DataHolder.getInstance().setCurrentText(button.getText().toString());
                    button.setText("");
                }
                return true;

            case MotionEvent.ACTION_UP:
                float endX = event.getX();
                float endY = event.getY();
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;

                if (isAClick(startX, endX, startY, endY) && clickDuration < CLICK_DURATION) {
                    v.performClick();
                }
                return true;
        }
        return false;
    }
});

Did someone see why my click is not triggered ?

Edit : the screenshot

enter image description here

zed13
  • 357
  • 4
  • 21

2 Answers2

0

This is my suggestion - Go to your phone settings there is an option to show layout boundaries you can set it on and off - you can see where you are clicking when this options is on .

I take no responsibility on what might happen to your phone yet this is how i always solve those problems

2D3D
  • 353
  • 1
  • 6
  • 22
  • Thanks for the proposition but I already know the option. The boundaries are great. As I said, there is no problem right know I just have an issue with my smartphone and I don't why, it's working on emulator and on my Nexus, so I can't figure out why... – zed13 Apr 01 '16 at 07:40
  • I think that something is covering the touchable area – 2D3D Apr 01 '16 at 08:05
0

I finally succeed to make my code working. I just added in the Action Move if the detection of a click, if it's not and only if it's not I begin a drag. That's it.

zed13
  • 357
  • 4
  • 21