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?