Open In App

How to Make a Phone Call From an Android Application?

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, let's build a basic android application that allows users to make phone calls directly from the app. This is accomplished with the help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers, and services, here used to make phone calls. This application will contain one activity with an EditText to fetch the phone number from the user input and a button to make a call.

Application to Make Phone Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Add Permission to AndroidManifest.xml File

You need to take permission from the user for a phone call and for that CALL_PHONE permission is added to the manifest file. Here is the code of the manifest file: 

<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.CALL_PHONE" />


Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. This file contains a LinearLayout which contains EditText to fetch the phone number from user input on which we can make a phone call and a button for starting intent or making calls: 

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:padding="64dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- Edit text for phone number -->
    <EditText
        android:id="@+id/editText"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:autofillHints="name"
        android:inputType="text"
        android:hint="Enter a phone number..." />

    <!-- Button to make call -->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginTop="32dp"
        android:text="Dial"
        android:textColor="@color/white"
        android:backgroundTint="@color/green"/>


</LinearLayout>

Layout:

Phone_Call_Layout



Step 4: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. In the MainActivity Intent, the object is created to redirect activity to the call manager, and the action attribute of intent is set as ACTION_CALL. Phone number input by the user is parsed through Uri and that is passed as data in the Intent object which is then used to call that phone number .setOnClickListener is attached to the button with the intent object in it to make intent with action as ACTION_CALL to make a phone call.

MainActivity File:

Java
package org.geeksforgeeks.demo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button button;

    private static final int CALL_PHONE_PERMISSION_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = editText.getText().toString();

                if (!phoneNumber.isEmpty()) {
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, 
                            Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        // Make the call
                        Intent phoneIntent = new Intent(Intent.ACTION_CALL);
                        phoneIntent.setData(Uri.parse("tel:" + phoneNumber));
                        startActivity(phoneIntent);
                    } else {
                        // Request permission
                        requestCallPermission();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void requestCallPermission() {
        ActivityCompat.requestPermissions(this, 
                new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_PERMISSION_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == CALL_PHONE_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
Kotlin
package org.geeksforgeeks.demo

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

class MainActivity : AppCompatActivity() {

    private lateinit var edittext: EditText
    private lateinit var button: Button

  	// Unique code to identify the permission request
    private val CALL_PHONE_PERMISSION_CODE = 100

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button = findViewById(R.id.button)
        edittext = findViewById(R.id.editText)

        button.setOnClickListener {
            val phone_number = edittext.text.toString()

            if (phone_number.isNotEmpty()) {
              	// Check if CALL_PHONE permission is already granted
                if (ActivityCompat.checkSelfPermission(
                        this,
                        Manifest.permission.CALL_PHONE
                    ) == PackageManager.PERMISSION_GRANTED
                ) {
                  	// Create an intent to initiate a phone call
                    val phone_intent = Intent(Intent.ACTION_CALL)
                    // Set phone number as intent data
                    phone_intent.data = Uri.parse("tel:$phone_number")
                    startActivity(phone_intent)
                } else {
                  	// Request the CALL_PHONE permission from the user
                    requestCallPermission()
                }
            } else {
                Toast.makeText(this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show()
            }
        }
    }

    /**
     * Request the CALL_PHONE permission from the user if not already granted.
     */
    private fun requestCallPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.CALL_PHONE),
            CALL_PHONE_PERMISSION_CODE
        )
    }

    /**
     * Handle the result of the permission request.
     */
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        
        // Check if the response matches the call phone permission request code
        if (requestCode == CALL_PHONE_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               	// Permission was granted
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show()
            } else {
               	// Permission was denied
                Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

Output:


Next Article
Practice Tags :

Similar Reads