How to Make a Phone Call From an Android Application?
Last Updated :
30 Jan, 2025
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:
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:
Similar Reads
How to Send an Email From an Android Application?
In this article, we will make a basic Android Application that can be used to send email through your android application. You can do so with the help of Implicit Intent with action as ACTION_SEND with extra fields:email id to which you want to send mail,the subject of the email, andbody of the emai
4 min read
Components of an Android Application
There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
How to Add Manifest Permission to an Android Application?
An AndroidManifest.xml file must be present in the root directory of every app project's source set. The manifest file provides crucial information about your app to Google Play, the Android operating system, and the Android build tools. Adding permissions to the file is equally important. In this a
2 min read
How to Create a NFC Reader and Writer Android Application
NFC that stands for Near Field Communication, is a very short range wireless technology that allows us share small amount of data between devices. Through this technology we can share data between an NFC tag and an android device or between two android devices. A maximum of 4 cm distance is required
9 min read
How to Build a ChatGPT Like Image Generator Application in Android?
Chat GPT is nowadays one of the famous AI tools which are like a chatbot. This chatbot answers all the queries which are sent to it. In this article, we will be building a simple ChatGPT-like android application in which we will be able to ask any question and from that question, we will be able to
5 min read
How to Handle Network Operations in an Android Application?
Handling network operations in an Android application is an essential task for developers. Network operations typically involve making HTTP requests to web services or APIs and receiving responses from them. These operations can include fetching data from a server, uploading files or data, authentic
5 min read
How to Launch an Application Automatically on System Boot Up in Android?
In many devices, we need our App to launch automatically when the device is started. In this article, we see how to add the auto-launch functionality to the App on the system boot. System Boot is а stаrt-uÑ sequenÑe thаt stаrts the оÑerаting system оf our device when it is turned оn. This annoying f
3 min read
How to Create Google Lens Application in Android?
We have seen the new Google Lens application in which we can capture images of any product and from that image, we can get to see the search results of that product which we will display inside our application. What are we going to build in this article? We will be building a simple application in w
10 min read
How to Share a Captured Image to Another Application in Android?
Pre-requisite: How to open a Camera through Intent and capture an image In this article, we will try to send the captured image (from this article) to other apps using Android Studio. Approach: The image captured gets stored on the external storage. Hence we need to request permission to access the
6 min read
How To Make An Android App
Have you always wanted to make an Android app but got lost in the complexity of the Android Studio or the technical jargon used in the development process? Don't worry, you have come to the right place. We are going to help you get started with your first Android app.The app that we are going to mak
6 min read