Tuesday, March 15, 2016

android - Make TextView normal text links clickable

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#c9d1d1"
    >
    <!--
        android:autoLink
            Controls whether links such as urls and email addresses are automatically found and
            converted to clickable links. The default value is "none", disabling this feature.

            Must be one or more (separated by '|') of the following constant values.
                none : Match no patterns (default).
                web : Match Web URLs.
                email : Match email addresses.
                phone : Match phone numbers.
                map : Match map addresses.
                all : Match all patterns (equivalent to web|email|phone|map).

            This corresponds to the global attribute resource symbol autoLink.
    -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="@android:color/black"
        android:text="My favorite search engine is google.com. My favorite site is http://www.yahoo.com."
        android:autoLink="web"
        />
</RelativeLayout>