- Android active link of url in TextView
- 10 Answers 10
- Not the answer you’re looking for? Browse other questions tagged android url textview or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How to Create a Hyperlink Using Android TextView
- Android TextView Hyperlink Tutorial
- Creating a String Resource for the Hyperlink in HTML Markup
- Creating an Activity Layout File with a TextView
- Enabling the TextView Hyperlink in the Activity or Fragment Class
- Java Example
- Kotlin Example
- 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
- Format TextView to look like link
- 7 Answers 7
- Make a hyperlink textview in android
- 7 Answers 7
Android active link of url in TextView
I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like hello . I have set the text using the following code.
And also set android:autoLink=»web» in the corresponding xml of that contains the TextView . Now the link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?
10 Answers 10
After revisiting all solutions, a summary with some explanations:
will find an URL and create a link even if android:linksClickable is not set, links are by default clickable. You don’t have to keep the URL alone, even in the middle of a text it will be detected and clickable.
To set a link via the code, same principle, no need for pattern or android:autoLink in layout, the link is found automatically using Linkify:
This works for me:
To Save any one time the true solution is
and i use it and it work perfect
There are 2 cases:
- the text looks like «click on http://www.hello.com»
then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:
- the text looks like click on hello
then you have to do it by code and tell the text is html, and specify a Link movement method for the click:
Check out this approach:
i have give some idea which i have found it
In your XML, you need to add android:linksClickable=»true» in the TextView.
If you are displaying in textview the string from strings.xml, strings containing the web link should not have word «a href=». If these words are deleted from the strings.xml file then the link will work.
Add these lines of code to your textView in xml File it will work perfectly fine..
android:autoLink=»web» android:textColorLink=»@android:color/holo_orange_dark» android:linksClickable=»true»
or if want a your own link in textview add these lines of code in your java file
set this ‘s’ String in your TextView by function
and don’t forget to add this line
id will be your textview id
Answer is right, BUT not complete, because I had in my xml some properties from other answers like autoLink and linksClickable and programatic way did not work. Also when I passes string with html from string resource also it did not work, so beware, you have to clean your xml and pass string directly exactly as in that answer.
I did not try it without without LinkMovementMethod but now I am ok as it finally work. Other answers did not work for me or was for visible url text, not clickable text as link.
Not the answer you’re looking for? Browse other questions tagged android url textview or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
How to Create a Hyperlink Using Android TextView
The TextView is a widget in Android that allows you to display text to the user. It is also possible to use the Android TextView widget to create a hyperlink which can load a web page in a mobile web browser when clicked.
To create a clickable hyperlink in your Android app using the TextView widget follow the steps below.
- Create a new String resource in the strings.xml resource file representing the text to be placed inside the TextView
- In your String resource use HTML markup to insert the hyperlink using an anchor tag
- Add a TextView in the layout of your Activity or Fragment and use the android:text attribute on the TextView to reference the String resource containing the hyperlink
- In your Activity or Fragment class use the setMovementMethod(…) on the TextView to enable the hyperlink
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
Android TextView Hyperlink Tutorial
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.
- Creating a String Resource for the Hyperlink in HTML Markup
- Creating an Activity Layout File with a TextView
- 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.
Creating a String Resource for the Hyperlink in HTML Markup
The first part of this tutorial for creating a hyperlink using the Android TextView widget will require you to add a new String Resource into the strings.xml file that will represent the text that will be used by the TextView for the hyperlink.
You will need to use HTML markup when writing your hyperlink in your resource file. This means you will need to use an anchor tag and use the href attribute on the anchor tag to capture the URI. Inside the anchor tag you will also need to add some text that will be displayed as a hyperlink to the user in the app.
You can also add a description to the link above or next to the link by adding some text outside of the anchor text in the String Resource. In my example I have also added a new line between my description of the link and the clickable link.
See the code sample of the strings.xml resource file below containing the hyperlink String resource.
Creating an Activity Layout File with a TextView
The next step in this tutorial will involve setting up the layout file for your Activity or Fragment that will contain the hyperlink TextView.
When you are creating the TextView in the layout file you will need to take care of a few things.
- Give the TextView an id using the “android:id” attribute so that you can reference it in the next part of the tutorial in your Activity or Fragment class
- Use the “android:text” attribute to reference the String resource you have created above for your hyperlink
- If you the text shown in the TextView is going to spill over one row consider centering the TextView by setting the “android:textAlignment” attribute to the value of “center”
See the code sample of the activity_main.xml layout resource file below containing a TextView to be shown inside a ConstraintLayout.
Enabling the TextView Hyperlink in the Activity or Fragment Class
The final step of this tutorial involves enabling the hyperlink to be clickable in your Activity or Fragment class by using the setMovementMethod(…) on the TextView.
The setMovementMethod(…) takes a MovementMethod as a parameter. In this tutorial we will be making use of the LinkMovementMethod allow the hyperlink to be clickable and to handle the launching of the web browser and navigation to the link in the browser when selected.
Java Example
See the code sample of the MainActivity.java Activity class file below showing the enabling of the hyperlink using setMovementMethod(…)
Kotlin Example
See the code sample of the MainActivity.kt Activity class file below showing the enabling of the hyperlink using setMovementMethod(…)
How to Change the Hyperlink Text Color in a TextView
By default the hyperlinks shown in your Android TextView widget will be displayed in a dark pink text color with an underline.
You can change the default color of the text of the hyperlink using the setLinkTextColor(…) method on the TextView.
See the code sample of the MainActivity.kt Activity class file below setting the color of the hyperlink in the TextView to blue using setLinkTextColor(…)
See a screenshot showing the hyperlink inside the TextView in blue using the code from the excerpt above.
How to Create a Clickable Link to a Website Using a Button in Android
You can also use a Button widget in Android to load a web page in the mobile browser when clicked.
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.
How to Create a Clickable Link to Another Activity in a TextView
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.
If you want to use a TextView instead of a Button to create a link to another Activity you can take the code samples from that lesson and replace the Button with a TextView. You will need to style the text shown in the TextView to look like a hyperlink by setting the text color to blue and adding an underline. There you will need to add an OnClickListener to create the Intent when the TextView is selected.
See the code excerpt from the IntentNavigationActivity.kt Activity class file below which shows setting the text color of the TextView to blue and adding an on click listener to switch to the MainActivity using an Intent when the TextView is clicked.
See the code sample of the activity_intent_navigation.xml layout resource file below containing a TextView to be shown inside a ConstraintLayout.
See the code sample of the strings.xml resource file below containing String Resource for the activity link which is underlined using HTML markup.
You will ensure need to use you have included references to all of the Activities you will be using and navigating between in your manifest file. This is done using the activity tag inside the application tag of the Android Manifest file.
Loading…
Источник
Format TextView to look like link
I’ve been using the android:autoLink just fine for formatting links and such, but I need to use android:onClick so I can’t use that in this case. The reasoning is that I find it too easy to click on a phone number accidentally, so I’m going to intercept the click with a confirmation Dialog and then call.
Is there an easy way to still make the phone number in my TextView look like a normal clickable link? I poked around the Android source code, but couldn’t find any particular style for me to reference.
7 Answers 7
This is the shortest solution:
Sadly, the effect of clicking doesn’t show up as being clicked on a real url link, but you can overcome it like so:
Another solution is to use Html.fromHtml(. ) , where the text inside has links tags («») .
If you wish for another solution, check this post.
- You can create a colors.xml resource file, what contains colors. Please take a look at Colors
- If you want to underline your text, then please take a look at this post: Underline
- Don’t forget to add android:clickable=»true» or setClickable(true) to your TextViews to make them clickable!
Linkify is a great class, it hunts for complex patterns like URLs, phone numbers, etc and turns them into URLSpans. Rather than re-write the existing regular expressions I extended the URLSpan class and created a method to upgrade only the telephone URLSpans to a custom URLSpan with a confirmation dialog.
First my extended URLSpan class, ConfirmSpan:
Next the method to swap out the different span classes:
Finally all you need to do is create a TextView with the autoLink attribute:
And remember to call the swapSpans() method. Understand that I wrote this for fun, there may be other methods of doing this but I am unaware of them at the moment. Hope this helps!
Источник
Make a hyperlink textview in android
I want to make a link for a textview text like Google. Is there anyway to make link like this. (i.e) When clicking on the word Google it should open the appropriate link. Any ideas are welcome.
7 Answers 7
Try this, and let me know what happen..
Using java code:
From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int) ,
Or in layout xml file, inside your TextView widget attributes
use android:autoLink=»web» in your TextView’s xml. It should automatically convert urls click-able (if found in text)
All tested and working 100%
Solution: android:autoLink=»web»
below is a complete example
Sample Layout Xml
String in string.xml
This can also be done by using the default property of Textview
Note :- Html.fromHtml is deprecated in Android N
You need to do check and support Android N and higher versions of Android
Alternatively
You can don’t want to id programmatically add autoLink flag on TextView.
This way You don’t need to add tags.
Which is a disadvantage, if you want to add hyperlink on a text you can’t do it this way. eg you can’t do something like this:- [hiteshsahu][1]
Источник