android


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());
}
}
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);
}
}

Related Links

checking the SQLite Connection Pool
Android Apps' content sharing works perfectly on emulator, but crashes on real device
RxJava filter behaviour
Correct sqlite query wrong output
Application name in data usage screen does not match Application Label
Can't have transparent background in OpenGL ES
Secure an API if needed
Android UI LinearLayout out of “phone screen”
How do I get the coordinates of an image same on all devices?
Recyclerview child items width
Keystore lost [Hypothetical scenario]. How to restore app
Couldn't resolve resource #drawable/email
Android notification start when ever I open app
How to identify Android Support Library versions in gradle?
How to move textViews between layouts in Android?
How to get id of view in the listview

Categories

HOME
algorithm
formal-languages
jwplayer
alarm
dojo
cakephp-3.4
gembox-spreadsheet
regular-language
gluon-mobile
joomla3.0
flatpak
bitnami
charles
inno-download-plugin
flask-sqlalchemy
google-api-client
beautifier
interbase
angularjs-components
linked-data
tpm
bringtofront
jaws
keras-layer
hunspell
html-encode
least-squares
numpy-broadcasting
video-capture
android-augmented-reality
apns-php
setup.py
trigonometry
branching-and-merging
plr
xmldocument
memory-mapping
uiimage
oim
flask-restful
dtsearch
tinymce-3
trojan
colorbar
android-testing
trigger.io
ampps
winusb
parfor
watch-os-2
jodd
dimension
shopping
nservicebus5
lua-telegram-bot
bizagi
data-protection
ikiwiki
false-sharing
maybe
inf
sapi
rate-limiting
intel-c++
canalyzer
viola-jones
sha512
liquid-layout
linked-tables
typeconverter
jquery-forms-plugin
xmltype
cryptographichashfunction
encryption-asymmetric
mobility
named-parameters
grails-domain-class
amslidemenu
getopt
multitasking
dtmf
workitem
nebula
pclzip
composite-component
mantle
twisted.web
bjyauthorize
monotouch.dialog
extconf.rb
multiplexing
screen-size
tmx
ie-compatibility-mode
updatesourcetrigger
hbm2java
addchild
dsl-tools
windows-identity
xpsdocument

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App