Search code examples
androidandroid-layoutandroid-xml

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());

Solution

  • 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"