I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my
onCreate
:OK I have answered my own question (but is it the best way?)
This is how to run a method when you click or tap on some text in a TextView:
and my main.xml is:
Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.
To use flat button, add
style="?android:attr/borderlessButtonStyle"
attribute -You can set the click handler in xml with these attribute:
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
MyActivity.java
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
from inside an activity that calls a layout and a textview, this click listener works:
the text view is declared like this (default wizard):
and in the strings.xml file