Wesley Egbertsen
Wesley Egbertsen

Reputation: 710

Onclick event on textview(that has TextIsSelectable="true") is ony called on second click

I have a onClickListener on a textview and the textview has the flag that it's selectable. But the onclick event I specified is only called when the textview is clicked a second time. After the second time it calles the onclick right, but if a other textview that also is selectable with a onclicklistener it also is only called the second time, then it works fine, but then the other one works only the second time again. I can't find the source of these weird events.

telefoonTXT.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {startTelIntent();}}
);

urlTXT.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {startWebIntent();}
});

Upvotes: 20

Views: 3709

Answers (4)

Vitaly
Vitaly

Reputation: 436

The accepted answer is good. But I had one additional issue with it: When I have some selection text in textview, I want to tap on textview to only deselecting the text - without perfoming on click listener. And perform on click only with no text selection. So below is my solution:

@SuppressLint("ClickableViewAccessibility")
public static void setOnClickGestureDetectorListener(TextView textView, Context context, View.OnClickListener onClickListener){
    if (textView == null || context == null || onClickListener == null)){
        return;
    }
    final GestureDetectorCompat detector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener());
    detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            onClickListener.onClick(textView);
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }
    });
    textView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ( v instanceof TextView ) {
                TextView textView = (TextView)v;
                if (textView.getSelectionStart() >= 0 && textView.getSelectionEnd() > textView.getSelectionStart()){
                    return false;
                }
                detector.onTouchEvent(event);
            }
            return false;
        }
    });
}

And calling example is below:

setOnClickGestureDetectorListener(textView, getContext(), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //  This is where u add your OnClick event     
                    }
                });

Upvotes: -1

Ultimo_m
Ultimo_m

Reputation: 4897

In my case focus change listener works, but be aware for cases when you use it in androidTv that you move with D-pad

textView.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus && textView.measuredHeight > textView.lineHeight * textView.lineCount) {
    //your click event               
    }
}

I check if text can be scrolled then don't take the click event

Upvotes: -1

Frosty
Frosty

Reputation: 500

I faced this issue as well. Whenever text view is touched firstly onTouch, then OnSelection and at last OnClick is called. If i understand your problem clearly you want to select text in text view when user double taps or long presses like the usual text selection but when user simply clicks it once u want the onClick to function. I think the following might help you.

Add a gestureDetector to your text View.

GestureDetectorCompat mDetector;
mDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener());

mDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // This is where u add your OnClick event
        startTelIntent();
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("dtttt", "double tap");
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }
});

telefoonTXT.setOnTouchListener(new View.OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
          mDetector.onTouchEvent(event);
          return false;
     }
});

Upvotes: 21

Smile
Smile

Reputation: 375

set this ...

TextisSelectable = "false"

i think it will be work correctly

Upvotes: -3

Related Questions