How to place hyperlink to a website on android app?
I want to place a hyperlink on android app that I am developing.
I tried this:
main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hyperlink"
android:id="@+id/hyperlink"
android:autoLink="web"
>
</TextView>
The strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebLink</string>
<string name="hyperlink">http://google.com</string>
</resources>
But the problem is, the link looks like this: http://google.com and I don't want to show the actual url.
1) How to replace link by text like "Click Here to visit Google" and the text is linked with the website url ?
2) How to place email address (same question, how to replace it with text something like "Click Here to Email" and the text should be linked with email@domain.com)
I also tried this tutorial: http://coder开发者_如何学Pythonzheaven.com/2011/05/10/textview-with-link-in-android/
But I am getting following error messages:
Description Resource Path Location Type
http cannot be resolved to a variable MyLink.java /MyLink/src/com/MyLink line 21 Java Problem
Syntax error on token "" <br /> <a href="", ? expected after this token MyLink.java /MyLink/src/com/MyLink line 21 Java Problem
Type mismatch: cannot convert from String to boolean MyLink.java /MyLink/src/com/MyLink line 20 Java Problem
Use the default Linkify class.
Here is an Example and the code from the tutorial:
This is my sample code for you, I think this will solve your problem:
public class StackOverflowActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 1) How to replace link by text like "Click Here to visit Google" and
// the text is linked with the website url ?
TextView link = (TextView) findViewById(R.id.textView1);
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page.";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
// 2) How to place email address
TextView email = (TextView) findViewById(R.id.textView2);
String emailText = "Send email: <a href=\"mailto:person@stackoverflow.com\">Click Me!</a>";
email.setText(Html.fromHtml(emailText));
email.setMovementMethod(LinkMovementMethod.getInstance());
}
}
精彩评论