How to open dialer in Android through Intent?
The phone dialer is an activity available with the Android operating system to call a number. Usually, such activity may or may not have an EditText, for taking the number as input, and a Call button. When the user presses the Call button, it invokes the dialer app activity. Use of 'tel:' prefix is recommended, else java.lang.IllegalStateException will be thrown. Action_Dial doesn't require any permission. Below is the code for MainActivity file in both Java and Kotlin.
Implementation:
Step 1: Adding permissions
In AndroidManifest.xml, include the below permission to directly call without opening in a dialer. To make a call through open in a dialer, below permission is not needed.
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
Step 2: Working with activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the following code. In order to make a direct call without switching into dialer activity, you need to add Intent.ACTION_CALL in place of Intent.ACTION_DIAL.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:hint="Enter your phone no."
android:inputType="phone"
android:padding="16dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/green"
android:text="Dial"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editText"
app:layout_constraintStart_toStartOf="@+id/editText"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout:
Step 3: Working with MainActivity.kt file
Navigate to app > kotlin+java > {package-name} > MainActivity.kt and make the following changes.
MainActivity File:
package org.geeksforgeeks.demo;
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 android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show();
// Use format with "tel:"
Uri uri = Uri.parse("tel:" + editText.getText().toString());
// Create the intent and set the data for the intent as the phone number.
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
try {
// Launch the Phone app's dialer with a phone number to dial a call.
startActivity(intent);
} catch (SecurityException s) {
// Show the toast with the exception message.
Toast.makeText(MainActivity.this, "An error occurred", Toast.LENGTH_LONG).show();
}
}
});
}
}
package org.geeksforgeeks.demo
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 android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<View>(R.id.editText) as EditText
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
Toast.makeText(this, "clicked", Toast.LENGTH_LONG).show()
// Use format with "tel:"
val uri = Uri.parse("tel:" + editText.text.toString())
// Create the intent and set the data for the intent as the phone number.
val intent = Intent(Intent.ACTION_DIAL, uri)
try {
// Launch the Phone app's dialer with a phone number to dial a call.
startActivity(intent)
} catch (s: SecurityException) {
// show() method display the toast with exception message.
Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG).show()
}
}
}
}