5

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:careers@bradfordcollege.ac.uk\">careers@bradfordcollege.ac.uk</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.

Talha Rahman
  • 720
  • 4
  • 12
  • 27
Michael Hanson
  • 323
  • 4
  • 12

2 Answers2

5

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: careers@bradfordcollege.ac.uk");

In your xml, you just need this property

android:autoLink="all"
HendraWD
  • 2,984
  • 2
  • 31
  • 45
0

You have to add your country code into Tel no. Like this :

careers_guidance_text.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:+4401274433043\">+441274 433043</a></p>
Email: <a href=\"mailto:careers@bradfordcollege.ac.uk\">
careers@bradfordcollege.ac.uk</a>"));

Also have to add CALL PHONE Permission in android manifest file. uses-permission android:name="android.permission.CALL_PHONE".

Talha Rahman
  • 720
  • 4
  • 12
  • 27
Tom
  • 1
  • 2