0

I have a couple Activities with TextViews that contain phone numbers. What I need is that when the user clicks on one, the phone dials the number. And by "dial", I mean dial, not bring up the dialer with the number preloaded, waiting for the user to click "Dial".

In other words, I want Intent.ACTION_CALL, not Intent.ACTION_DIAL.

Now keep in mind, I'm entirely new to Android development, and I'm just beginning to figure out how to put things together.

My first attempt was simple. In my XML:

<TextView
    android:id="@+id/textPhoneNumber"
    android:text="@string/the_phone_number"
    android:autoLink="phone"
    android:clickable="true"
    android:onClick="clickPhone"
    />

Then in my java:

public void clickPhone(View view)
{
    String phoneNumber = getString(R.string.the_phone_number);
    Uri uri = Uri.parse("tel:" + phoneNumber);

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(uri);
    startActivity(callIntent);
}

Having read more about how autolink is supposed to work, that's clearly more work than should be necessary, but it did work...

Until I had to do the same with a phone number that included letters. You know the kind: 1-800-BUY-MY-STUFF.

The problem, Linkify.PHONE_NUMBERS doesn't recognize strings containing letters as phone numbers, so my string would not be rendered as a link. The onClick would still file, though, and I'd still get the behavior I wanted.

So, I went digging into what autoLink actually did, and read up a bit on Linkify, and removed my onCLick and tried to use a custom pattern:

Pattern matchEverything = Pattern.compile("^.*$");
TextView textPhoneNumber = (TextView)findViewById(R.id.textPhoneNumber);
Linkify.addLinks(textPhoneNumbermatchEverything, "tel:");

Since my TextView contained only a phone number, I figured I'd keep the Regex simple. And this worked fine.

Except that I think I am getting Intent.ACTION_DIAL instead of Intent.ACTION_CALL.

What's the simplest way to get phone number to display in an Activity as a clickable link, and to have clicking that link dial the phone, instead of just bringing up the dialer?

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
  • Linkify is supposed to provide Intent.ACTION_DIAL facility. So that host app doesn't require call permission at all. If you need Linkify with Intent.ACTION_CALL then you need to simulate the Linkify behaviour by your own custom code. – Amit K. Saha Apr 23 '15 at 06:12

1 Answers1

1

You can just skip the whole linkify business by simulating the same behavior:

Use SpannableString to underline the text in TextView:

String udata="1-800-BUY-MY-STUFF";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

then in XML, set the TextView's text color to #05c5cf

After that it's a matter of calling your clickPhone method.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • Finally got around to trying this out. Seems to work OK, but the color is off. My links are #35B6E5. What seems to work best is mTextView.setTextColor(mTextView.getLinkTextColors().getDefaultColor()); – Jeff Dege Apr 27 '15 at 20:06