Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make phone numbers clickable in a textview in Android

I have a text view that contains an email and a phone number. My desired effect is that when the user taps the email, it opens the default emailing application and when the user taps the phone number, the application pulls it up in the dialer. With the code below:

XML:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/careers_guidance"
            android:id="@+id/careers_guidance_text"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginTop="5dp"
            android:linksClickable="true"
            android:textColorLink="#0000EE"
            android:autoLink="all"/>

Java:

careersGuidance = view.findViewById(R.id.careers_guidance_text);
    careersGuidance.setText(Html.fromHtml("<p>Help with choosing the right course and with thinking about where this might take you in the future.</p>" +
            "<p>Tel: <a href=\"tel:01274433043\">01274 433043</a></p>Email: <a href=\"mailto:[email protected]\">[email protected]</a>"));

When I run my application, only the email is clickable and works the way I want it to work, the phone number isn't clickable or highlighted.

enter image description here

However, I have noticed that if I remove the email address from the setText java code, and run the application. The phone number becomes blue and underlined as if it is clickable, but when I tap it, nothing happens.

How can I get this working?

Also, I have CALL_PHONE permissions granted in my manifest file, and also on my emulator device.

like image 334
Michael Hanson Avatar asked Apr 16 '19 21:04

Michael Hanson


1 Answers

From the email (.ac.uk), I suppose that you are from UK. You just need to add your country phone number prefix to replace 0, which is +44. And you don't need to use Html.fromHtml.

careersGuidance.setText("Help with choosing the right course and with thinking about where this might take you in the future.\n\nTel: +441274433043\n\nEmail: [email protected]");

In your xml, you just need this property

android:autoLink="all"
like image 88
HendraWD Avatar answered Oct 12 '22 18:10

HendraWD