Android Studio Phone Call
Syntax
To make a phone call from Android Studio, you need to perform the following steps:
Check for permission: Add the
CALL_PHONE
permission in theAndroidManifest.xml
file.Create an
EditText
and aButton
element in the layout XML file.Set an
onClick
listener on theButton
element.In the
onClick
method, retrieve the phone number from theEditText
element and create aUri
object with thetel
scheme.Create an
Intent
object withACTION_CALL
action and theUri
object as data.Start the activity with the
startActivity()
method.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
String phoneNumber = phoneNumberEditText.getText().toString().trim();
if (!TextUtils.isEmpty(phoneNumber)) {
Uri dialUri = Uri.parse("tel:" + phoneNumber);
Intent callIntent = new Intent(Intent.ACTION_CALL, dialUri);
startActivity(callIntent);
}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
Example
Suppose you have an app where the user can enter a phone number and click a button to make a phone call. Here is an example of how to implement this feature in Android Studio.
Layout XML File
<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
Activity Java File
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
private EditText phoneNumberEditText;
private Button callButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
callButton = findViewById(R.id.callButton);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makePhoneCall();
}
});
}
private void makePhoneCall() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
String phoneNumber = phoneNumberEditText.getText().toString().trim();
if (!TextUtils.isEmpty(phoneNumber)) {
Uri dialUri = Uri.parse("tel:" + phoneNumber);
Intent callIntent = new Intent(Intent.ACTION_CALL, dialUri);
startActivity(callIntent);
}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makePhoneCall();
} else {
Toast.makeText(this, "Call permission was not granted", Toast.LENGTH_SHORT).show();
}
}
}
Output
When the user enters a phone number and clicks the "Call" button, the phone app will be opened and begin dialing the number.
Explanation
To make a phone call from Android Studio, you need to add the CALL_PHONE
permission in the AndroidManifest.xml
file, create an EditText
and a Button
element in the layout XML file, set an onClick
listener on the Button
element, retrieve the phone number from the EditText
element and create a Uri
object with the tel
scheme, create an Intent
object with ACTION_CALL
action and the Uri
object as data, and start the activity with the startActivity()
method.
To handle the case where the user has not granted the CALL_PHONE
permission, you need to check for the permission using checkSelfPermission()
, and if it is not granted, request the permission using requestPermissions()
. You also need to override the onRequestPermissionsResult()
method to handle the result of the permission request.
Use
The Android Studio Phone Call feature can be used in any app where the user needs to make a phone call.
Important Points
The
CALL_PHONE
permission is a dangerous permission, so you need to ask for it at runtime on devices running Android 6.0 (API level 23) and higher.Before making a phone call, you should check if the
CALL_PHONE
permission is granted. If it is not granted, you should request it.The
ACTION_CALL
intent is used to start the default phone app and begin dialing a phone number.
Summary
In this guide, we learned how to make a phone call from Android Studio by adding the CALL_PHONE
permission in the AndroidManifest.xml
file, creating an EditText
and a Button
element in the layout XML file, setting an onClick
listener on the Button
element, retrieving the phone number from the EditText
element and creating a Uri
object with the tel
scheme, creating an Intent
object with ACTION_CALL
action and the Uri
object as data, and starting the activity with the startActivity()
method. We also learned how to handle the case where the user has not granted the CALL_PHONE
permission, and important points to keep in mind when implementing the Android Studio Phone Call feature.