5

Here I used clickable URL on Textview and it works. But how can I set clickable and highlighted Text with Open URL with a browser. It's possible from Android XML OR kotlin without using java code like setText(Html.fromHtml("")).

String value = "<html>Visit Web <a href=\"http://www.domainname.com\">mysite</a> View</html>";
    TextView text = (TextView) findViewById(R.id.text);
    text.setText(Html.fromHtml(value));
    text.setMovementMethod(LinkMovementMethod.getInstance());
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Jigar Patel
  • 1,550
  • 2
  • 13
  • 29
  • Possible duplicate of [How do I make links in a TextView clickable?](https://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – Andy Developer Jun 30 '17 at 06:43
  • Doing it «without using java» is impossible because `TextView` is written in Java. – Miha_x64 Jun 30 '17 at 08:10

8 Answers8

7

enter image description here

<TextView
        android:textSize="18sp"
        android:autoLink="web"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="http://www.example.com"
        tools:ignore="HardcodedText" />

It's No required declear layout id and no code from java side. it's works from xml

Use Autolink

    • For website android:autoLink="web"
    • For call android:autoLink="phone"
    • For email android:autoLink="email"
    • For map android:autoLink="web"
    • For all android:autoLink="all"
Jigar Patel
  • 1,550
  • 2
  • 13
  • 29
5

This property works for you TextView: android:autoLink="web"

here is an example Layout layout.xml

<TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtDefaultpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>
Adnan Maqbool
  • 452
  • 3
  • 10
4

Just add this attribute to your TextView android:autoLink="web" e.g

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:autoLink="web"
        android:clickable="true"
        android:text="https://www.google.co.in"/>
Manish Singh Rana
  • 822
  • 1
  • 13
  • 26
2

Add line to the TextView:

`android:autoLink="web"` 

Like this

<TextView
        ................
        ................
        android:autoLink="email"
       .................
 />
Kush
  • 1,080
  • 11
  • 15
1

as @Aakash Verma suggested use android:autoLink="web".

but it will change your textColor. To over-come this also add android:textColorLink="your_text_color" to you TextView.

Your TextView should look like.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="https://www.google.co.in/"
    android:textColor="@color/colorPrimary"
    android:textColorLink="@color/colorPrimary" /> 

If you still want to do in in java

myUrl= (TextView) rootView.findViewById(R.id.myUrl);
myUrl.setText("https://www.google.co.in/");

Linkify.addLinks(myUrl, Linkify.WEB_URLS);
myUrl.setLinkTextColor(Color.parseColor("#000000"));

EDIT:

enter image description here enter image description here

Happy Coding

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • What is the look of a link or how would it get distinguished from the normal text if it continues with the same colour? – Aakash Verma Jun 30 '17 at 06:00
  • if you just use `android:autoLink="web"` it will change your text color to your Accent color. and if you haven't specified Accent color.. it will be changed to Sky Blue (Default). `android:textColorLink="your_text_color"` wiill work as `android:textColor` for your link. i'm uploading SS for more understanding. – V-rund Puro-hit Jun 30 '17 at 06:09
0

Use android:autoLink="web" in your TextView's xml. It should automatically convert urls click-able (if found in text)

Also, don't forget android:linksClickable="true". I don't think that you need to set html tag like you did, just the <a href> tag...

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
0

In case you are using text from strings.xml resource, it seems that the html tags gets stripped out.

So you have to use <![CDATA[**your string with click links**]]> in the strings.xml to convert it to html using Html.fromHtml(string)

Monster Brain
  • 1,950
  • 18
  • 28
0

Add this to your Textview:

android:autoLink="web"
android:linksClickable="true"

Ajaz Ahmed
  • 315
  • 4
  • 18