Open In App

How to Make a Phone Call From an Android Application?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, you will make a basic android application that can be used to call some numbers through your android application. You can do so 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 use to make phone calls. This application basically contains one activity with edit text to write the phone number on which you want to make a call and a button to call that number.

Step By Step 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: 

<!-- Permission for phone call -->
<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 Relative Layout which contains EditText to write the phone number on which you want to make a phone call and a button for starting intent or making calls: 

XML




<?xml version="1.0" encoding="utf-8"?>
<!--Relative Layout-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Edit text for phone number -->
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />
      
    <!-- Button to make call -->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="115dp"
        android:padding="5dp"
        android:text="Make Call!!" />
</RelativeLayout>


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. Here is the complete code: 

Java




import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // define objects for edit text and button
    EditText edittext;
    Button button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Getting instance of edittext and button
        button = findViewById(R.id.button);
        edittext = findViewById(R.id.editText);
  
        // Attach set on click listener to the button for initiating intent
        button.setOnClickListener(arg -> {
            // getting phone number from edit text and changing it to String
            String phone_number = edittext.getText().toString();
  
            // Getting instance of Intent with action as ACTION_CALL
            Intent phone_intent = new Intent(Intent.ACTION_CALL);
  
            // Set data of Intent through Uri by parsing phone number
            phone_intent.setData(Uri.parse("tel:" + phone_number));
  
            // start Intent
            startActivity(phone_intent);
        });
    }
}


Kotlin




import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
      
    // define objects for edit text and button
    private lateinit var edittext: EditText
    private lateinit var button: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Getting instance of edittext and button
        button = findViewById(R.id.button)
        edittext = findViewById(R.id.editText)
  
        // Attach set on click listener to the button for initiating intent
        button.setOnClickListener(View.OnClickListener {
            // getting phone number from edit text and changing it to String
            val phone_number = edittext.text.toString()
  
            // Getting instance of Intent with action as ACTION_CALL
            val phone_intent = Intent(Intent.ACTION_CALL)
  
            // Set data of Intent through Uri by parsing phone number
            phone_intent.data = Uri.parse("tel:$phone_number")
  
            // start Intent
            startActivity(phone_intent)
        })
    }
}


Output:

Make a Phone Call from an Android Application

 



Last Updated : 30 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads