Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the value of link text when clicked in a textview in android
    text
    copied!<p>I have a <strong>TextView</strong>. I have added custom links like <strong>"@abc"</strong>, <strong>"#android"</strong> by matching some regex pattern. The links are displaying properly. However I am not getting a way to extract the text of the link which is clicked. I am using <strong>SpannableString</strong> to setText to the textview. I then set spans using my custom <strong>ClickableSpan</strong>. It works fine. Plus I can also catch the onclick event. But the <strong>onClick</strong>() method has a View paramter. If I call <strong>getText()</strong> on the View (ofcourse after typecasting it to TextView), it returns the entire text. I searched a lot but always found ways to add links and catch the event, but none told about getting the text of the link.</p> <p>This is the code I am using to add links and recieve onclick. I got the code from one of the SO threads..</p> <pre><code>Pattern pattern = Pattern.compile("@[\\w]+"); Matcher matcher = pattern.matcher(tv.getText());//tv is my TextView while (matcher.find()) { int x = matcher.start(); int y = matcher.end(); final android.text.SpannableString f = new android.text.SpannableString( tv.getText()); f.setSpan(new InternalURLSpan(new View.OnClickListener() { public void onClick(View v) { showDialog(1); } }), x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(f); tv.setLinkTextColor(Color.rgb(19, 111, 154)); tv.setLinksClickable(true); </code></pre> <p>Here is the InternalURLSpan:</p> <pre><code>class InternalURLSpan extends android.text.style.ClickableSpan { View.OnClickListener mListener; public InternalURLSpan(View.OnClickListener listener) { mListener = listener; } @Override public void onClick(View widget) { mListener.onClick(widget); TextView tv = (TextView) widget; System.out.println("tv.gettext() :: " + tv.getText()); Toast.makeText(MyActivity.this,tv.getText(), Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Is it possible to get the text of the link clicked? If not, is there a way of associating some data to a particular link and knowing which link gets clicked? Any pointers.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload