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

How to capture sound while doing video capture with Android Studio?
How to get altitude from Google Maps
How to resize images to fit a fixed size inside a CardView in Android?
Can't connect Android to desktop server
Intent Camera Crashes on Nexus Devices
Simplest Method for Android App with rtmp Video
Android Bitmap usage Optimization
Android | Google Sign | Get IdToken correctly in silent
Integrate firebase notifications topics and firebase authentication
Nesting a ScrollView inside a TableRow
AndroidStudio order mixed up when using Intents to open Activities
Prevent Joda-Time to print negative numbers
Getting email from Facebook - FB android SDK
Pick an image and save it in custom folder
Android Insistent Notification Interrupted By SMS - How to make uninterruptable?
Android: Send notifications to all users

Categories

HOME
algorithm
xml
microsoftgraph
adal
time-complexity
apache-flink
rdbms
twig
mysql-workbench
pdfbox
vxml
zip
trac
gkturnbasedmatch
coin3d
dreamweaver
fileserver
ksh
backpack-for-laravel
monit
jquery-callback
roundup
appirater
ms-access-web-app
soot
spotipy
scrollview
userdefaults
code-snippets
gnat-gps
classcastexception
vue-chartjs
carriage-return
apiary.io
openerp-7
setup.py
vmware-fusion
skylink
naming
movilizer
microsoft-metro
importerror
ntvs
sonarlint-eclipse
configurationmanager
launchctl
payara-micro
colorbar
opengl-4
oozie-coordinator
materialized-path-pattern
pacemaker
tinkerpop3
rspec2
wso2developerstudio
dimension
halcon
toast
mobiledoc-kit
xenomai
oracle-spatial
lazybones
jdi
forwarding
video-player
airbrake
linked-tables
canvg
diff3
beatsmusic
jama
named-parameters
template-deduction
cuba
personalization
getopt
isqlquery
cocoalibspotify-2.0
printdialog
font-awesome-4.0.0
insert-id
jquery-blockui
zope.interface
correlated-subquery
boost-signals
fusefabric
mapping-by-code
httponly
fitbounds
ikimagebrowserview
exponentiation
presentation-layer
silent
isapi-extension
tabpanel
murmurhash
wordprocessingml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App