0

I use linkify to make a textview work as hyperlink, and it does work nicely. The only issue is the underline is missing, could anyone point me out what could cause the problem? shouldn't the underline come by default?

Thanks!

Green Ho
  • 881
  • 3
  • 14
  • 27

2 Answers2

2

Take a look at the Spannable params

addLinks(Spannable text,...)

linkify class

SpannableStringBuilder class

-replying to comment- SpannableStringbuilder implements CharSequence, which can be used in TextView.setText(); So once you finish making your underlined text, you can use TextView.setText() and still use the method your are using.

Or refer to this: How to set underline text on textview?

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • I use addLinks(TextView text, Pattern p, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter) – Green Ho Aug 30 '12 at 19:50
  • I figured out that it's because I used UrlSpan instead of UnderlineSpan... is that possible I can set both? – Green Ho Aug 30 '12 at 20:39
  • Yes, you can do it with the SpannableStringBuilder class, you can set as many spans as you want, as long as they don't override each other. – wtsang02 Aug 30 '12 at 20:46
  • Thanks! Is there a way I can retrieve the existing SpannableString from a textView so that I can add the new span to it? – Green Ho Aug 30 '12 at 21:11
  • String yourString=someTextView.getText().toString(); – wtsang02 Aug 30 '12 at 21:12
  • I tried: Spannable s = (Spannable) myTextView.getText(); s.setSpan(...); s.setSpan(...), but it doesn't work – Green Ho Aug 30 '12 at 21:20
  • please set as answer if you got it you work. check the check mark next to the answer. – wtsang02 Oct 28 '12 at 14:16
0

You can use the xml attribute autoLink="web" for the TextView widget to automatically detect if the content is a web address. Here is an example:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:textColorLink="@color/hyperlink_blue"
    android:autoLink="web"
    android:textSize="16sp"/>

The attribute textColor is for text other than hyperlinks, which will be 'black' in the above example and the attribute textColorLink is for any text that takes a form of a hyperlink - which will be blue per above.

You can also append other autoLink values by 'piping' them together:

android:autoLink="web|email|map|phone"

This works for TextView, AppCompatTextView (SupportV7/AppCompat), AppCompatTextView (androidx/AppCompat)

Ram Iyer
  • 1,621
  • 1
  • 23
  • 25