How to set a hyperlink in interactive text? Android Java

I have a textView with a single url. But I donโ€™t want to show the whole URL, but just a few words like: Click here. And when the text box is clicked. The application needs to open the URL "behind" the words. Press here.

For information: Now TextView can be clicked. URL is displayed correctly. When the URL is clicked, the browser will launch and load the URL correctly.

I only want to change the link text, which is visible.

EDIT: Each time the application is reloaded or reloaded, the URL may be different, so it always has the same URL.

+6
source share
3 answers

use below code: -

android:autoLink="web" 

like this

 <TextView android:id="@+id/txt_post_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:autoLink="web" android:text="" android:textColor="@color/wall_msg" android:textSize="16sp" /> 
+5
source

Have you tried using html to achieve this? So, for example, you should specify the following as text:

 String text = "<a href='www.link.com'>Click here</a>"; textView.setText(Html.fromHtml(text)); 
+3
source

Change text in text form to whatever you like. And provide the url as a variable in the code

EDIT: You say the url is changing: so just save the url in a variable. It would be interesting how your application will receive a new url

0
source

Source: https://habr.com/ru/post/969565/


All Articles