Reputation: 1120
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
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
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
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
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
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
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