Android textview html link

The task itself is easy: You have a TextView which should show a clickable link to open a WebPage. As ususal, there are several ways to achieve the goal. The nasty thing is: if you mix them, they might no longer work. And also, some sometimes work, sometimes they don’t.

As it took me a little while to figure my final settings, I’ll note them here. A StackOverflow Post was a hint into a very good direction.

First, display the links as clickable links in the textview:

Now there are two options:
Either make the link clickable programmatically by adding the following code:

textView.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());

or add android:autoLink=»web» to the according XML-element:

If one of the options doesn’t work, try the other one, but don’t mix them. In one of my apps there are TextViews in two different activities with very similar setup. Yet, the android:autoLink=»web» solution didn’t work in both TextViews whereas the setMovementMethod works in both. In the StackOverflow Post, some users also mention that both solutions didn’t work, and that android:linksClickable=»true» was a solution.

My learning was: there are multiple ways to achieve the goal, but test it on each component separately as I do not yet completely trust the setMovementMethod way.

Читайте также:  Ограничение количества символов в строке python

I have a question about changing the color of the hyperlink of a text. Can you provide some solutions about this?? Thanks in advance.

I have a question about changing the color of the hyperlink of a text. Can you provide some solutions about this?? Thanks in advance.

Источник

Learn to Droid Logo

In the article below I will include Java and Kotlin code samples that show how to create a hyperlink using the Android TextView following the steps listed above.

I have also included some additional instructions along with code samples written in Kotlin for using links in Android apps in different ways such as:

  • How to Change the Hyperlink Text Color in a TextView
  • How to Create a Clickable Link to a Website Using a Button in Android
  • How to Create a Clickable Link to Another Activity in a TextView

Below is a screenshot of an Android app we will be building in this tutorial for creating a hyperlink using the Android TextView widget. When the hyperlink is selected the app will open a mobile web browser and navigate the user to the website https://learntodroid.com

In this tutorial will cover the following steps for creating a hyperlink using the Android TextView widget.

  1. Creating a String Resource for the Hyperlink in HTML Markup
  2. Creating an Activity Layout File with a TextView
  3. Enabling the TextView Hyperlink in the Activity or Fragment Class

I will include code excerpts embedded into this tutorial. For step 3, I have included code samples for both Kotlin and Java.

I have created a video tutorial embedded below on how to create a hyperlink inside a TextView in an Android app. The code written in this video tutorial is also available on GitHub.

You can also use a Button widget in Android to load a web page in the mobile browser when clicked.

Читайте также:  Python воспроизвести wav файл

To do this you will need to make use of an Intent and use the startActivity(…) method to launch an Intent when the Button is clicked.

See the code sample of the MainActivity.kt Activity class file below showing how to use a Button to launch a web browser on the device at a given URI when clicked.

I have written another blog post on this website about how to switch between Activities in Android using an Intent. To learn more about this including an example using a Button to switch between Activities using an Intent check out the article below.

Loading…

Источник

To make a TextView clickable(hypertext link) in Android you can do it in value Strings file or calling setMovementMethod(). There are multiple scenarios and conditions.

Let’s see some situations which we have found on stack overflow:

A. android:autoLink=»web» works if you have full links in your HTML. The following will be highlighted in blue and clickable:

B. view.setMovementMethod(LinkMovementMethod.getInstance()); will work with the following (will be highlighted and clickable):

Note:- that the third (A-3) option has a hyperlink, but the description of the link (the part between the tags) itself is not a link. android:autoLink=»web» does NOT work with such links.

android:autoLink=»web» if set in XML will override view.setMovementMethod(LinkMovementMethod.getInstance()); (i.e.; links of the third kind will be highlighted, but not clickable).

If using a view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and then make sure you don’t have android:autoLink=»web» in your XML layout if you want all links to be clickable.

Step 1. Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio).

Step 2. Open the strings.xml file.

make links in a TextView clickable

Step 3. Add this text in the strings.xml file.

Step 4. Add the following code in activity_mail.xml

Using a LinearLayout and 2 TextView fields for both examples.

Читайте также:  Python add spaces to string

Step 5. Add the below code in MainActivity.kt

package com.eyehunts.androidtextviewclickable import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.LinkMovementMethod import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView_link.setMovementMethod(LinkMovementMethod.getInstance()) >>

Step 6. Now Run the application, in an emulator or on your Android device.

Output screenshot Android TextView link underline example:

Android TextView link underline

Download Complete Code TextView clickable

Answer: You can do it with this java code.

TextView textView =(TextView)findViewById(R.id.textView); textView.setClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); String text ; textView.setText(Html.fromHtml(text));

From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int) ,

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

Or in the layout XML file, inside your TextView widget attributes

android:autoLink="web" android:linksClickable="true"

Do comment if you knew another way to do it or have any problem in this tutorial.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=25″
TargetSdkVersion=28″Coding in Kotlin

Источник

Hyper Linking is the most usable method for all website because with the use of hyperlink website developer can insert links on their web pages and on each & every link holds a unique web address. Now with the use of spanned variable android developers can also insert hyperlinks in android apps using textview tag. So here is the complete step by step tutorial for Add Hyperlink in android application through textview.

android-project-download-code-button

Code for MainActivity.java file.

package com.textviewhyperlink_android_examples.com; import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.widget.TextView; public class MainActivity extends Activity < TextView HyperLink; Spanned Text; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HyperLink = (TextView)findViewById(R.id.textView1); Text = Html.fromHtml("Click on this link to visit my Website 
" +
"Android-Examples.com"); HyperLink.setMovementMethod(LinkMovementMethod.getInstance()); HyperLink.setText(Text); > >

Code for activity_main.xml layout file.

Screenshots:

Add Hyperlink in android application through textview

open link

Источник

Оцените статью