Make the string clickable underlined in TextView

enter image description here

I want to make the line "this link" underlined and clickable, but I do not know how to achieve it.

XML file:

<string name="submitText">Before you submit, please check out &lt;u>this link&lt;/u></string> 

In my snippet:

 tvSubmit.setText(Html.fromHtml(getString(R.string.submitText))); 

I do not want the entire line to be clicked, but only the underlined section. I can’t use a horizontal LinearLayout with 2 cells, because on smaller devices the line will not continue to look, it will be divided into 2 cells.

What I tried:

 tvSubmit.setMovementMethod(LinkMovementMethod.getInstance()); Spannable sp = (Spannable) tvSubmit.getText(); ClickableSpan click = new ClickableSpan() { @Override public void onClick(View widget) { showLink(); } }; sp.setSpan(click, 0, sp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

In the above code, an entire line is underlined, and the text color changes to blue.

+12
android textview hyperlink underline
source share
6 answers

the problem is that you set span to the entire line ( sp.setSpan(click, 0, sp.length() ). To fix this, you should set only the click range on this link . I did the same:

 <string name="submitText">Before you submit, please check out %1$s</string> <string name="this_link">this link</string> 

in your activity

 String thisLink = getString(R.string.thisLink); String yourString = getString(R.string.submitText, thisLink); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(yourString); spannableStringBuilder.setSpan(click, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

where startIndex and endIndex are the thisLink index in yourString . I split the two lines because it was easier to search for indexes, especially if you have to deal with translations. To calculate startIndex, you can use yourString.indexOf(thisLink) , and endIndex startIndex + the length of thisLink . I will leave you the usual checks (negative indexes and everything else that might cause an IndexOutBoundException )

+10
source share

you can define strings.xml in your files

 <string name="submitText">Before you submit, please check out <a href="actual url">this link</a> 
+3
source share
  <TextView android:id="@+id/tvSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:autoLink=@String/submitText //link the content of web android:textColorLink="#576586" //change the color of the link android:textColor="#555555" /> 

in activity // sample

 String webLinkText = <a href="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png"><img src="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png" alt="Screenshot-Mozilla Firefox (Private Browsing)" width="293" height="254" class="alignnone size-full wp-image-291" /></a> tvSubmit = (TextView) findViewById(R.id.tvSubmit); tvSubmit.setText(Html.fromHtml(webLinkText))); 

check for a more detailed answer

+2
source share
 sp.setSpan(click, 0, sp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

change value 0: with a link to the start of a position in the text, an example of a change value: 10 if you set 0, it will be a hyperlink to the entire text.

0
source share

just made this function to help me add clickable text in a textview:

 public static SpannableStringBuilder getSpannablableClickFor(CharSequence initialText, ClickableSpan click, String textToAppend) { String description = String.format("%s %s", initialText, textToAppend); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(description); int start = description.indexOf(textToAppend); int end = start + textToAppend.length(); spannableStringBuilder.setSpan(click, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.WHITE); spannableStringBuilder.setSpan(fcs, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableStringBuilder; } 
0
source share

You can try using the code below.

 String str = "Before you submit, please check out this link"; tvSubmit.setMovementMethod(LinkMovementMethod.getInstance()); tvSubmit.setText(setClickablePart(str), BufferType.SPANNABLE); 

To handle the Click event in text mode below Method

  private SpannableStringBuilder setClickablePart(String str) { SpannableStringBuilder m_spannableStringBuilder = new SpannableStringBuilder(str); int m_index = str.indexOf("this"); final String clickString = str.substring(m_index, str.length()); m_spannableStringBuilder.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this, clickString, Toast.LENGTH_SHORT).show(); } }, m_index, str.length(), 0); return m_spannableStringBuilder; } 
-one
source share

All Articles