Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 protected])


I also tried this tutorial: http://coderzheaven.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
like image 329
super Avatar asked Jul 14 '11 16:07

super


1 Answers

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:[email protected]\">Click Me!</a>";
        email.setText(Html.fromHtml(emailText));
        email.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
like image 54
kameny Avatar answered Sep 22 '22 04:09

kameny