Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClickableSpan strange behavior:onClick() called when clicking empty space

I have a TextView with ClickableSpan in that both layout_height and layout_width is wrap_content.

When the text in TextView in not too long, it work fine. When the text is long enough such that it take 2 line, it also work fine but have some strange behavior.

That is when I clicked on the second line's empty space(not fill with text yet but part of TextView) , the ClickableSpan onClick() callback is called.

I do not expect this as I clicked the empty space only but not the spanned text. Although it does not affect much, I want to know what is behind.

I set the ClickableSpan with below code:

TextView tv = (TextView) findViewById(R.id.text);
tv.setText("TEXT TEXT TEXT TEXT");
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(tv.getText());
ssb.setSpan(new TestClickableSpan(), ssb.length()-5, ssb.length(), 0);
tv.setText(ssb);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setOnTouchListener(new TextViewOnTouchListener());

The TextViewOnTouchListener:

class TextViewOnTouchListener implements OnTouchListener{

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TextView", "onTouch");
            return false;
        }       
}

The TestClickableSpan:

class TestClickableSpan extends ClickableSpan{

        @Override
        public void onClick(View arg0) {
            Log.d("ClickableSpan", "Confirm OnClick: "+arg0.toString());
        }       
    }
like image 498
Yeung Avatar asked Feb 14 '12 09:02

Yeung


1 Answers

I managed to solve it by extending LinkMovementMethod, and check whether the touch event offset is equals or bigger than the text lenght:

public class MovementMethod extends LinkMovementMethod {

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            if (off >= widget.getText().length()) {
               // Return true so click won't be triggered in the leftover empty space
                return true;
            }
        }

        return super.onTouchEvent(widget, buffer, event);
    }
}
like image 75
dor506 Avatar answered Oct 02 '22 17:10

dor506