DeathRs
DeathRs

Reputation: 1120

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

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" />

Upvotes: 4

Views: 3198

Answers (6)

IshRoid
IshRoid

Reputation: 3735

Do nothing in Code just include android:autoLink="web" into your Textview XML like

<Textview . ..... .....
          android:autoLink="web"/>

Above code will work to make any link clickable in TextView String...

Upvotes: 3

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28517

You can use Html.fromHtml to convert html tags in your string to links and appropriate formatting and setMovementMethod enables automatic handling of link clicks.

TextView tv = (TextView)findViewById(R.id.textView);
String s = getString(R.string.strname);
tv.setText(Html.fromHtml(s));
tv.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 1

manDroid
manDroid

Reputation: 1135

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.

Upvotes: 3

SmulianJulian
SmulianJulian

Reputation: 802

 YOURTEXTVIEWNAME.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

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

    });

The terms here you want to know are intent, onClickListener and URI. Welcome newbie.

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157447

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

Upvotes: 5

thestrongenough
thestrongenough

Reputation: 225

final TextView view = (TextView) findViewById(R.id.textview);
view.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// request your webservice here. Possible use of AsyncTask and  ProgressDialog
// show the result here - dialog or Toast
}

};);

I think this would help you.

And the Most importantly In android view you can define onclick service for any item in view

Upvotes: 0

Related Questions