Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Textview clickable, so that it opens the url in the web browser

Tags:

android

sorry for asking this newbie question, as i am new to android development. what code should be entered in the main.java?

<resources>
<string name="strname">Clickable Text<a href="http://domain.com">Visit Website</a></string>
</resources>



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:autoLink="web"
android:text="@string/strname"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
like image 268
DeathRs Avatar asked Dec 12 '22 01:12

DeathRs


2 Answers

you can use Linkfy:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://www.google.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);

From the documentation

Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links

like image 132
Blackbelt Avatar answered May 24 '23 00:05

Blackbelt


On textview click event write this:

 Uri uri = Uri.parse("http://domain.com");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

that open the your URL.

like image 39
manDroid Avatar answered May 24 '23 00:05

manDroid