Ackman
Ackman

Reputation: 1592

How to set a TextView in Android as a link? I don't want to display the link but something else like "click me"

I am trying to add a URL on my Android App login page which redirects user to recover password.

Upvotes: 0

Views: 91

Answers (2)

Shadab Ansari
Shadab Ansari

Reputation: 7070

I guess you want to show the user a text looking like a url so that when the user taps on it you can redirect him to a web page. In your xml layout, declare a TextView with text attribute set

<TextView android:id = "@+id/txt"
...

android:text= "Click me !">

And in your activity class,

txt = (TextView)findViewById(R.id.txt);
SpannableString content = new SpannableString(txt.getText());
content.setSpan(new UnderlineSpan(), 0, txt.length(), 0);
txt.setText(content);
txt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("http://www.google.com"));
        startActivity(i);
    }
});

Upvotes: 1

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

You are thinking in terms of how a web page would do it, but it doesn't necessarily have to be a text link like a web page. You could simply have a button (or any view really) that takes the user to password recovery when touched.

As an example, this is how you would do it with a button in your layout:

Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String url = "http://www.your-password-recovery-page.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

This will launch the default browser of the device and navigate to your password recovery url.

Upvotes: 0

Related Questions