"+number+""; Linkify.addLinks(t, Linkify.PHONE_NUMBERS); Html.fromHtml(value);" /> "+number+""; Linkify.addLinks(t, Linkify.PHONE_NUMBERS); Html.fromHtml(value);" /> "+number+""; Linkify.addLinks(t, Linkify.PHONE_NUMBERS); Html.fromHtml(value);"/>

I need to call a phone number from my android app? Which one if efficient from the below

74 Views Asked by At

1) I have used linkify html method link

 value = "<a href="+to_tel+">"+number+"</a>";
 Linkify.addLinks(t, Linkify.PHONE_NUMBERS);
 Html.fromHtml(value);`                

2) Clickable Span method

ClickableSpan clickableSpan = new ClickableSpan() {
     @Override
     public void onClick(View textView) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
        startActivity(intent); 
 }


SpannableString spannableString = new SpannableString(stringBuilder);
spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
1

There are 1 best solutions below

0
Rahul Khurana On

The second approach is best which is using the intent. As it is officially recommended in Android documentation.

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
startActivity(intent); 

Note:

Places a phone call (requires the CALL_PHONE permission)

<uses-permission android:name="android.permission.CALL_PHONE" />