Skip to content
Related Articles

Related Articles

Improve Article

How to open dialer in Android through Intent?

  • Difficulty Level : Basic
  • Last Updated : 03 Nov, 2020
Geek Week

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.

Java




package com.geeksforgeeks.gfg.dial;
  
// importing packages
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
  
        // Binding MainActivity.java with 
        // activity_main.xml file
        setContentView(R.layout.activity_main);
      
    }
  
    // This function is called when button is clicked.
    public  void Call(View v)
    {
        // Find the EditText by its unique ID
        EditText e = (EditText)findViewById(R.id.editText);
  
        // show() method display the toast with message 
        // "clicked"
        Toast.makeText(this, "clicked", Toast.LENGTH_LONG)
             .show();
  
        // Use format with "tel:" and phoneNumber created is 
        // stored in u.
        Uri u = Uri.parse("tel:" + e.getText().toString());
  
        // Create the intent and set the data for the 
        // intent as the phone number.
        Intent i = new Intent(Intent.ACTION_DIAL, u);
  
        try
        {
            // Launch the Phone app's dialer with a phone 
            // number to dial a call.
            startActivity(i); 
        }
        catch (SecurityException s)
        {
            // show() method display the toast with 
            // exception message.
            Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG)
                 .show();
        }
    }
}

Kotlin




package com.geeksforgeeks.gfg.dial
  
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.gfgreviewinjava.R
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
  
        // Binding MainActivity.java with
        // activity_main.xml file
        setContentView(R.layout.activity_main)
    }
  
    // This function is called when button is clicked.
    fun Call(v: View?) {
        // Find the EditText by its unique ID
        val e = findViewById<View>(R.id.editText) as EditText
  
        // show() method display the toast with message
        // "clicked"
        Toast.makeText(this, "clicked", Toast.LENGTH_LONG)
                .show()
  
        // Use format with "tel:" and phoneNumber created is
        // stored in u.
        val u = Uri.parse("tel:" + e.text.toString())
  
        // Create the intent and set the data for the
        // intent as the phone number.
        val i = Intent(Intent.ACTION_DIAL, u)
        try {
            // Launch the Phone app's dialer with a phone
            // number to dial a call.
            startActivity(i)
        } catch (s: SecurityException) {
            // show() method display the toast with
            // exception message.
            Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG)
                    .show()
        }
    }
}

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




<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
  
    <!-- covers entire width of the screen -->
    android:layout_width="match_parent"
  
    <!-- covers entire height of the screen -->
    android:layout_height="match_parent"
  
    tools:context="com.example.hp.dial.MainActivity">
  
    <EditText
        android:id="@+id/editText"
        <!-- covers as much width as required. -->
        android:layout_width="wrap_content"
  
        <!-- covers as much height as required. -->
        android:layout_height="wrap_content"
  
        <!-- left spacing from the parent layout-->
        android:layout_marginLeft="8dp"
  
        <!-- right spacing from the parent layout-->
        android:layout_marginRight="8dp"
  
        <!-- top spacing from the parent layout-->
        android:layout_marginTop="65dp"
  
        <!-- hint works as a place holder -->
        android:hint="Phone No."
  
        <!-- Expressing the given input should be phone no -->
        android:inputType="phone"
  
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <Button
        android:id="@+id/button"
  
        <!-- covers as much width as required. -->
        android:layout_width="wrap_content"
  
        <!-- covers as much height as required. -->
        android:layout_height="wrap_content"
  
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="67dp"
  
        <!-- name of function is Call, and it is -->
        <!-- invoked when the button is clicked.-->
        android:onClick="Call"
  
        android:text="DIAL"
  
        <!-- below are the positions of the button -->
        <!-- with respect to editText and parent layout. -->
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
    />
</android.support.constraint.ConstraintLayout>

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-permission android:name="android.permission.CALL_PHONE"

 

Figure 1. Enter Phone no.

 

Figure 2. Open the dialer activity.

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up