Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 643
user1891910 Avatar asked Nov 03 '22 05:11

user1891910


2 Answers

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);
    }
}
like image 58
karabara Avatar answered Nov 09 '22 13:11

karabara


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;
        }
    }
}
like image 23
Ola Ström Avatar answered Nov 09 '22 11:11

Ola Ström