Is it possible to use dragging events and click event on the same Textview?
Asked Answered
N

2

7

In my application I got a requirement to use both click and drag events on the same textView.

I have written the following code:

... }
    switch(event.getAction()) {
        case MotionEvent.ACTION_UP:
//            TextDialog.setVisibility(View.VISIBLE);
            break;
        case MotionEvent.ACTION_DOWN: {
            disallowTouch(parent, true);
            int downX = (int)event.getX();
            int downY = (int)event.getY();
            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE:
            int x = (int)event.getRawX();
            int y= (int)event.getRawY();

            layoutParams.leftMargin = x - 50;
            layoutParams.topMargin = y - 70;
                                       
            tvText.setLayoutParams(layoutParams);
            break;
        default:
            break;
    }
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    TextDialog.setVisibility(View.VISIBLE);
}

But only ACTION_MOVE is working. The onClick event is not getting fired.

I just want to display a dialog when clicking on the TextView.

How can I achieve this?

Negation answered 1/2, 2013 at 7:45 Comment(0)
E
2

Use setOnTouchListener and setOnClickListener simultaneously

in onTouch:

public boolean onTouch(View v, MotionEvent event) {
    ViewParent parent = v.getParent();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        disallowTouch(parent, true);
        downX = event.getX();
        downY = event.getY();
        return false; // allow other events like Click to be processed
    }
}

private void disallowTouch(ViewParent parent, boolean isDisallow) {
    if (parent != null) {
        parent.requestDisallowInterceptTouchEvent(isDisallow);
    }
}
Expiration answered 1/2, 2013 at 7:59 Comment(6)
as karabara says, your onTouch method should return false;Timmytimocracy
Change your MotionEvent.ACTION_MOVE block it should return falseExpiration
I changed,now onClick is fired,but it is getting called whenever I drag and leave it also,I dont want to show this when I drag and leftNegation
I don't know what exactly you trying to do, but I think you could achieve it by correctly implement onTouch event. I've gave simply example where ACTION_DOWN is handling, but may be you should think about another types of MotionEventExpiration
I have done as you said,but both the events are firing at a time,I want to drag the text,but when I drag and leave then Click event is getting fired and my dialog got displayed,I dont want to do like this,I want only touch event to perform so that I can drag the text to some place.Similarly when I try to click on text,touch event is firing.Negation
I think this is another question. If you want to implement dragging you could find solutions here on stackoverflow or on github, or you can implement it yourself. The main idea is to handling drag states in onTouch method.Expiration
L
1

I don't think you should be using MotionEvent.ACTION_DOWN but only MotionEvent.ACTION_UP:

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        // OnClick
        case MotionEvent.ACTION_UP: {
            TextDialog.setVisibility(View.VISIBLE);                
            return false;  // To allow other events
        }
        // OnTouch
        case MotionEvent.ACTION_MOVE: {
            ClipData clipData = ClipData.newPlainText("", "");
            View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
            view.startDragAndDrop(clipData, dragShadowBuilder, null, 0);
            return true;
        }
    }
}
Landmark answered 26/1, 2022 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.