Can I use event drag and click event in the same Textview?

In my application, I have a requirement to use both click and drag events in the same textView.

I wrote 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 works, the onClick event does not fire.

I just want to display a display dialog when I click on a TextView.

How can i achieve this?

+5
source share
1 answer

Use setOnTouchListener and setOnClickListener at the same time

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);
    }
}
+1
source

All Articles