Wednesday, January 3, 2018

Android Open Phone Dialer from Code


There are Two ways to achieve it.

 1) Have to start the dialer via code, without user interaction. 

 You need Action_Dial, use below code it will open Dialer with number specified.

Intent intent = new Intent(Intent.ACTION_DIAL); 
intent.setData(Uri.parse("tel:0123456789")); 
startActivity(intent);

The 'tel:' prefix is required, otherwhise the following exception will be thrown:

java.lang.IllegalStateException: Could not execute method of the activity. Action_Dial doesn't require any permission. If you want to initiate the call directly without user's interaction , You can use action Intent.ACTION_CALL.

In this case, you must add the following permission in your AndroidManifest.xml:

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

2) Have user to click on Phone_Number string and start the call. 

android:autoLink="phone" 

You need to use TextView with below property.
android:autoLink="phone" android:linksClickable="true"

This is how you can open EditText label assigned number on dialer directly.
<TextView
 android:id="@+id/phoneNumber"
 android:autoLink="phone"
 android:linksClickable="true"
 android:text="+91 22 2222 2222"
 />
Ref: https://stackoverflow.com/questions/11699819/how-do-i-get-the-dialer-to-open-with-phone-number-displayed