Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make clickable url link in TextView on android without using java

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());
like image 384
Jigar Patel Avatar asked Jun 30 '17 04:06

Jigar Patel


2 Answers

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"
like image 192
Jigar Patel Avatar answered Nov 15 '22 04:11

Jigar Patel


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:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</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>
like image 22
Adnan Maqbool Avatar answered Nov 15 '22 04:11

Adnan Maqbool