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
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.
Creating a HyperlinksetText("http://example.com"); link. setOnAction((ActionEvent e) -> { System. out. println("This link is clicked"); });
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With