Make textview hyperlink in android

I want to create a link for text text like Google . One way or another, to make such a link. (ie) When you click on the word Google, he should open the corresponding link. Any ideas are welcome.

+54
android url textview hyperlink android-textview
Feb 15 '12 at 9:19
source share
6 answers

Try this, and let me know what will happen.

TextView textView =(TextView)findViewById(R.id.textView); textView.setClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); String text = "<a href='http://www.google.com'> Google </a>"; textView.setText(Html.fromHtml(text)); 
+95
Feb 15 2018-12-12T00:
source share

use android:autoLink="web" in your TextView xml. It should automatically convert URLs with clicks (if they are found in the text)

+40
Feb 15 2018-12-12T00:
source share

All tested and working 100%
Solution: android:autoLink="web"
below is a complete example

Xml Layout Example

  <TextView android:id="@+id/txtLostpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:autoLink="email" android:gravity="center" android:padding="20px" android:text="@string/lostpassword" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/txtLostpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:autoLink="web" android:gravity="center" android:padding="20px" android:text="@string/defaultpassword" android:textAppearance="?android:attr/textAppearanceSmall" /> 

String in string.xml

 <string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string> <string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string> 
+25
Feb 25 '14 at 2:33
source share

This can also be done using the default Textview property .

 android:autoLink="email" 
+5
May 23 '14 at 13:21
source share

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

+1
Mar 16 '17 at 10:36
source share

For latest SDK fromHtml deprecated Use bottom line

 String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>"; if (Build.VERSION.SDK_INT >= 24) { textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY)); } else { textView.setText(Html.fromHtml(yourtext)); } 
0
Nov 01 '17 at 6:25
source share



All Articles