Android active link of url in TextView
After revisiting all solutions, a summary with some explanations:
android:autoLink="web"
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.
<TextView
android:text="My web site: www.stackoverflow.com"
android:id="@+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:autoLink="web">
</TextView>
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:
final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
myClickableUrl.setText("Click my web site: www.stackoverflow.com");
Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
How to make links in a TextView clickable
Buried in the API demos, I found the solution to my problem:
File Link.java:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
I removed most of the attributes on my TextView to match what was in the demo.
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtCredits"/>
That solved it. It is pretty difficult to uncover and fix.
Important: Don't forget to remove autoLink="web"
if you are calling setMovementMethod()
.
how to make clickable url link in TextView on android without using java
<TextView
android:textSize="18sp"
android:autoLink="web"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="http://www.example.com"
tools:ignore="HardcodedText" />
It's No required declear layout id and no code from java side. it's works from xml
Use Autolink
- For website
android:autoLink="web"
- For website
- For call
android:autoLink="phone"
- For call
- For email
android:autoLink="email"
- For email
- For map
android:autoLink="web"
- For map
- For all
android:autoLink="all"
- For all
How to link text in a TextView to open web URL
I solved my problem simply by the following code.
Kept HTML-like string:
<string name="urltext"><a href="https://www.google.com/">Google</a></string>
Made layout with NO link-specific configuration at all:
<TextView
android:id="@+id/link"
android:text="@string/urltext" />`Added the MovementMethod to the TextView:
mLink = (TextView) findViewById(R.id.link);
if (mLink != null) {
mLink.setMovementMethod(LinkMovementMethod.getInstance());
}
Now it allows me to click the hyperlinked text "Google" and now opens web browser.
This code was from vizZ answer at the following linked Question
Android - Enabling only URL link, but disabling the others when it touches something non-URL link in TextView
You need to handle links programatically. This way you will have way to separate clickable spans and non-clickable spans. And you can add more code on click of spans if you want.
Below is the way which you can use
private void createUrlSpansInTextView(TextView tv, String text) {
tv.setText(text); // Or you can remove this line if you already set text to textview
SpannableString current=(SpannableString)tv.getText();
URLSpan[] spans=
current.getSpans(0, current.length(), URLSpan.class);
for (URLSpan span : spans) {
int start=current.getSpanStart(span);
int end=current.getSpanEnd(span);
current.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL()), start, end,
0);
}
}
Where CustomURLSpan
is
private static class CustomURLSpan extends URLSpan {
public CustomURLSpan(String url) {
super(url);
}
@Override
public void onClick(View widget) {
// Write your code to load urls
}
}
What if I want to place pictures in the middle of text? I guess it is
impossible with TextView. How to make it?
No with TextView not possible. You need to look for third party libraries. I am not sure if any exists.
Make a hyperlink textview in android
Try this, and let me know what happen..
Using java code:
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));
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 layout xml file, inside your TextView widget attributes
android:autoLink="web"
android:linksClickable="true"
TextView catch clicking on link when autoLink is set to web
Actually,
I solved this by abandoning autolLink='web'
and by parsing html url, creating SpannableString
and setting additional spans.
So, let's say that I want to do the following:
- if user clicks on the part of the text of textView which is not url, method
someMethod()
should be called - if user clicks on the part of the text of textView which is url, method
someMethod()
should be called and browser should be openned with given URL
So, first we have the content which is in the variable message
and method String extractFirstLink(String text)
will be used to get the url.
private static String extractFirstLink(String text) {
Matcher m = Patterns.WEB_URL.matcher(text);
return m.find() ? m.group() : null;
}
Note: in my case I know that I will have only one url, but if you can expect more than one url, this solution (found somewhere on StackOverflow, can't find it right now) will do the work:
public static String[] extractLinks(String text) {
List<String> links = new ArrayList<String>();
Matcher m = Patterns.WEB_URL.matcher(text);
while (m.find()) {
String url = m.group();
links.add(url);
}
return links.toArray(new String[links.size()]);
}
So, here is the code:
int contentTextColor = ...; // color of non-url text in textView
int urlTextColor = ...;
String message = notification.getMessage(); // content stored
final String foundLink = extractFirstLink(message);
SpannableString styledString = new SpannableString(message);
if (foundLink != null) {
int startPosition = message.indexOf(foundLink);
int endPosition = startPosition + foundLink.length();
styledString.setSpan(new ForegroundColorSpan(urlTextColor), startPosition, endPosition, 0);
ClickableSpan clickableURLSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
someMethod();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(foundLink));
widget.getContext().startActivity(browserIntent);
}
};
ClickableSpan clickableNonURLSpan1 = new ClickableSpan() {
@Override
public void onClick(View view) {
someMethod();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
};
ClickableSpan clickableNonURLSpan2 = new ClickableSpan() {
@Override
public void onClick(View view) {
someMethod();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
};
styledString.setSpan(clickableURLSpan, startPosition, endPosition, 0);
if (startPosition > 0) {
styledString.setSpan(clickableNonURLSpan1, 0, startPosition - 1, 0);
styledString.setSpan(new ForegroundColorSpan(contentTextColor), 0, startPosition -1, 0);
}
if (endPosition < message.length() -1) {
styledString.setSpan(clickableNonURLSpan2, endPosition + 1, message.length(), 0);
styledString.setSpan(new ForegroundColorSpan(contentTextColor), endPosition + 1, message.length(), 0);
}
txtView.setMovementMethod(LinkMovementMethod.getInstance());
}
txtView.setText(styledString);
There are some interesting things here. For example, when I wish to make span responsive to click, I have to use ClickableSpan
, but! I must handle situation where I have format text1-url-text2. So, since all these three parts should be ClickableSpan
s and text1 and text2 should not have underline, which is the default behaviour of it, I had to override updateDrawState
method in my implementation of ClickableSpan
for text1 and text2.
There is another catch, I had to create two implementation of ClickableSpan interface in order to have to work as intended. That's why I created clickableNonURLSpan1
and clickableNonURLSpan2
.
Related Topics
How to Make Space Between Linearlayout Children
Setting Action Bar Title and Subtitle
How to Run a Specific Android App Using Terminal
Showing The Current Selection in a Listview
Install_Failed_Cpu_Abi_Incompatible on Device Using Intellij
Android Detect Webview Url Change
Android - Open Target _Blank Links in Webview with External Browser
Fragmentmanager Is Already Executing Transactions. When Is It Safe to Initialise Pager After Commit
Syncadapter Without a Contentprovider
When Using Alertdialog.Builder with Edittext, The Soft Keyboard Doesn't Pop
Android Volley Imageloader - Bitmaplrucache Parameter
How to Add a Custom Item to a Navigationview with a Menu Layout
How to Store Object in Sqlite Database
How to Import an Existing Project from Github into Android Studio