Dialer Intent - Android Studio - Java

How to launch dialer pad with a specific phone number using Intent

Learn how to easily open the phone dialer or make a direct call in your Android app using Kotlin. This step-by-step guide explains how to use Intent.ACTION_DIAL and Intent.ACTION_CALL, request runtime permissions, and write clean Kotlin code in Android Studio. Ideal for beginners and professional Android developers looking to integrate calling features into their apps.

>> Check For Compose

Video:

Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dialer Intent"
        android:textStyle="bold" />

    <!--Edit text to input number/code-->
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Enter Number/Code...">

        <EditText
            android:id="@+id/numberEt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone" />

    </com.google.android.material.textfield.TextInputLayout>

    <!--Button to dial number/code-->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/dialBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:minWidth="120dp"
        android:text="Dial" />

</LinearLayout>

MainActivity.java

package com.technifysoft.myapplication;

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 {

    //Views
    EditText mNumberEt;
    Button mDialBtn;

    //String variable to store text from edit text
    String number = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initialize views
        mNumberEt = findViewById(R.id.numberEt);
        mDialBtn = findViewById(R.id.dialBtn);

        //button click to dial
        mDialBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get text from edittext
                number = mNumberEt.getText().toString().trim();

                if (number.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Enter Number...!", Toast.LENGTH_SHORT).show();
                } else {
                    //Dialer Intent
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Uri.encode(number)));
                    startActivity(intent);
                }

            }
        });

    }

}

Step 3: Screenshots






Comments

Popular posts from this blog

Picture In Picture - Android Studio - Kotlin

Manage External Storage Permission - Android Studio - Kotlin

SeekBar with Customization | Android Studio | Java