Smartphones are always more and more powerful but at the end, the main usage of a smartphone remains calling some people to communicate with them. In this tutorial, you are going to discover how to make calls programmatically on Android.

You can also discover this tutorial in video on YouTube :

First, we need to create a layout with an EditText view to enter the phone to call and a Button letting user to start calling when he will click on it :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.ssaurel.phonetut.MainActivity">

  <EditText
    android:id="@+id/number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"/>

  <Button
    android:id="@+id/dial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/number"
    android:layout_marginTop="20dp"
    android:text="Dial"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

Then, we must add the CALL_PHONE permission in the Android Manifest. Besides, like we target Android Marshmallow (API 23 and above), we will need to manage permissions at runtime.

Now, we can start to write the Java code of the main activity. First, we get references for the EditText and the Button views. Then, we add a ClickListener on the Button. When a user will click on the button, we will access to the phone number entered.

If the phone number field is not empty, we check of the CALL_PHONE permission has been granted for the application by creating a checkPermission method like that :


private boolean checkPermission(String permission) {
  return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
}

If the permission has been granted, we have just to create an Intent with the Intent.ACTION_CALL in parameter and the phone number to call.

If the permission has not been granted previously for our application, we need to request the permission by calling the static requestPermissions method of the ActivityCompat class like that :


ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 
  MAKE_CALL_PERMISSION_REQUEST_CODE);

[java]

To manage the result of this request, we need to override the onRequestPermissionsResult and to check if permission has been granted :

[java]

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  switch(requestCode) {
    case MAKE_CALL_PERMISSION_REQUEST_CODE :
      if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
        dial.setEnabled(true);
        Toast.makeText(this, "You can call the number by clicking on the button", Toast.LENGTH_SHORT).show();
      }
    return;
  }
}

Finally, the complete code of the MainActivity class will have the following form :


package com.ssaurel.phonetut;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  private static final int MAKE_CALL_PERMISSION_REQUEST_CODE = 1;
  private Button dial;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dial = (Button) findViewById(R.id.dial);
    final EditText numberToDial = (EditText) findViewById(R.id.number);

    dial.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String phoneNumber = numberToDial.getText().toString();

        if (!TextUtils.isEmpty(phoneNumber)) {
          if (checkPermission(Manifest.permission.CALL_PHONE)) {
            String dial = "tel:" + phoneNumber;
            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
          } else {
            Toast.makeText(MainActivity.this, "Permission Call Phone denied", Toast.LENGTH_SHORT).show();
          }
        } else {
          Toast.makeText(MainActivity.this, "Enter a phone number", Toast.LENGTH_SHORT).show();
        }
     }
   });

   if (checkPermission(Manifest.permission.CALL_PHONE)) {
     dial.setEnabled(true);
   } else {
     dial.setEnabled(false);
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MAKE_CALL_PERMISSION_REQUEST_CODE);
   }
  }

  private boolean checkPermission(String permission) {
   return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
  } 

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode) {
      case MAKE_CALL_PERMISSION_REQUEST_CODE :
        if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
          dial.setEnabled(true);
          Toast.makeText(this, "You can call the number by clicking on the button", Toast.LENGTH_SHORT).show();
        }
        return;
    }
  }
}

Last step is to click on the Run Application button on Android Studio and clicking on the button to start calling the phone number entered :