Android active link of url in TextView
To Save any one time the true solution is
<TextView
android:text="www.hello.com"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web"
android:linksClickable="true">
</TextView>
and i use it and it work perfect
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);
This works for me:
<TextView
android:text="www.hello.com"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web">
</TextView>
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:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="click on http://www.hello.com"/>
- the text looks like
click on <a href="http://hello.com">hello</a>
then you have to do it by code and tell the text is html, and specify a Link movement method for the click:
String text = "click on <a href=\"http://hello.com\">hello</a>";
TextView textView = view.findViewById(R.id.textView);
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());