Search code examples
androidclickablephone-call

How to make a android textview autolink its value as a phone number


So Ive currently made a working app that when the user puts in an employees name and searches it searches my sql database and returns with a name, phone number, id and address all in a list so that each result is kinda separated from the next. Im trying to make the phone number auto link as a phone number so that the user can click on it and it will call it but Im unsure of how to do this. Yes I have followed and looked at the other tutorials for that on this site and haven't been able to make it work. hoping you guys can help me out. For starters, yes I have added the permission to the manifest.

Here is the xml layout for the textview I wish to make clickable

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Phone Number"
    android:id="@+id/textType"
    android:layout_marginLeft="5dp"
    android:layout_below="@id/textSize"
    android:textColor="#666"
    android:clickable="true" />

And here the java that setups up how the number will be displayed. Not sure if this is needed or even helps.

myHolder.textType.setText("Mobile Phone# " + current.Phonenum);

Solution

  • Try adding the android:autoLink="phone" attribute, so your code should look like this:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="123 456 7890"
        android:id="@+id/textType"
        android:layout_marginLeft="5dp"
        android:layout_below="@id/textSize"
        android:autoLink="phone"
        android:textColor="#666"
        android:clickable="true"
    />