Hi and welcome to another tutorial from Codingdemos. In this tutorial, you will learn how to use Android call intent to make a phone call with a button click without using permission. Open Android Studio and start coding 🙂
By the end of this tutorial, you will have an app that looks like this. (Large preview)
In this tutorial we will be using the following:
Android Studio version 4.1.2
Minimum SDK API 23
1- Open Android Studio.
Android studio 4.1.2 welcome screen. (Large preview)
2- Open activity_main.xml file, and add a Android button. This button will be used to initiate the call.
< Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call" />
3- Let’s add some margin to the button from the top using android:layout_marginTop=”200dp”.
< Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="Call" />
4- Next, will change the button color to Green using android:backgroundTint=”@color/colorAccent”.
< Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:backgroundTint="@color/colorAccent"
android:text="Call" />
5- Build and run the app to see the button.
Android Changed Button Color. (Large preview)
6- Open MainActivity.java file. Here you will be doing most of the work to make the button clickable to initiate a phone call programmatically.
7- First, you need to declare and reference the call button.
Button buttonCall = findViewById(R.id.btnCall);
8- Next, you will need to use setOnClickListener to make the button clickable.
buttonCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
9- Now you can use one of Android Intent called Intent.ACTION_CALL to start the Android call intent without permission.
Intent intent = new Intent(Intent.ACTION_DIAL);
10- You will need to pass a phone number into the Android call intent in order for it to work and initiate the call. You can do that by using Android setData method.
intent.setData(Uri.parse("tel:123456789"));
Note: You need to include ‘tel:’ before the number in order to initiate the call. Failing to do so will result in the following exception: “android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=123456789 }”
11- Finally, you can start the intent using Android startActivity so that you can trigger the intent to call the number (123456789).
startActivity(intent);
12- Build and run the app to see the result.
Android make phone call programmatically. (Large preview)
13- Here is the full code for Activity_main.xml file.
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
< Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:backgroundTint="@color/colorAccent"
android:text="Call" />
< /LinearLayout>
14- Here is the full code for MainActivity.java file.
package com.codingdemos.codingdemos;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button buttonCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCall = findViewById(R.id.btnCall);
buttonCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
}
});
}
}
15- I hope you find this tutorial helpful, and if you have any questions, please post them in the comment below.