Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control onclicklistener in autolink enabled textview

Tags:

android

I am using a TextView for which I have set autolink="web" property in XML file. I have also implemented the onClickListener for this TextView. The problem is, when the text in TextView contains a hyperlink, and if I touch that link, the link opens in browser but simultaneously the onClickListener triggers too. I don't want that.

What I want is, if I touch the hyperlink the clickListener should not fire. It should only fire if I touch the part of the text that is not hyperlinked. Any suggestion?

like image 976
Adnan Avatar asked Sep 02 '10 06:09

Adnan


People also ask

Can I make a TextView clickable?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.

How do I enter TextView?

for the new line in TextView just add \n in middle of your text it works..


1 Answers

You can achieve this using a work around in getSelectionStart() and getSelectionEnd() functions of the Textview class,

tv.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {         ClassroomLog.log(TAG, "Textview Click listener ");         if (tv.getSelectionStart() == -1 && tv.getSelectionEnd() == -1) {             //This condition will satisfy only when it is not an autolinked text             //Fired only when you touch the part of the text that is not hyperlinked          }     } }); 

It may be a late reply, but may be useful to those who are searching for a solution.

like image 194
binary Avatar answered Sep 23 '22 00:09

binary