Note: - Html.fromHtml is deprecated in Android N
You need to check and support Android N and higher versions of Android
//Set clickable true tagHeading.setClickable(true); //Handlle click event tagHeading.setMovementMethod(LinkMovementMethod.getInstance()); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY)); } else { tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>")); }
As an alternative
You may not want to programmatically add an AutoLink flag to a TextView.
Android: AutoLink = "Web"
Android: linksClickable = "true"
This way you do not need to add <a href='somelink'> tags.
Which is a drawback, if you want to add hyperlink to text , you cannot do it this way. for example, you cannot do something like this: - [ hiteshsahu ] [1]
<TextView android:id="@+id/tag_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tag_ll" android:layout_gravity="left" android:layout_margin="10dp" android:autoLink="web" android:linksClickable="true" android:text="https://github.com/hiteshsahu" android:textColor="@color/secondary_text" />
The result of both approaches: -
https://github.com/hiteshsahu
Hitesh Sahu Mar 16 '17 at 10:36 2017-03-16 10:36
source share