Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make normal links in TextView clickable?

In my Android app, I have a TextView. The text can contain links. This is an example of a text:

This is just a test. Click the following link http://www.google.com to visit Google.

Note that the text is not in HTML; it will be just a regular text.

I want to do something like textView.parseLinks(), then in the TextView, http://www.google.com will be hyper-linked and clickable to open up the page.

Is this possible?

Thanks

like image 883
omega Avatar asked Aug 14 '13 02:08

omega


People also ask

Can a TextView be clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do you make a clickable link in Java?

Creating a HyperlinksetText("http://example.com"); link. setOnAction((ActionEvent e) -> { System. out. println("This link is clicked"); });

How do I make a hyperlink?

Create a hyperlink to a location on the webSelect the text or picture that you want to display as a hyperlink. Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box.


2 Answers

Try and include the following in the TextView definition in XML file:

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

The docs of android:autoLink say:

Controls whether links such as urls and email addresses are automatically found and converted to clickable links

So for automatically finding links, the above may help. Try and see.

like image 56
Shobhit Puri Avatar answered Nov 12 '22 01:11

Shobhit Puri


Something like this should work.

    TextView tv = (TextView) findViewById(R.id.textView1);
    String text = "This is just a test. Click this link here <a href=\"http://www.google.com\">Google</a> to visit google.";
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setText(Html.fromHtml(text));
like image 6
Ye Lin Aung Avatar answered Nov 12 '22 02:11

Ye Lin Aung