How to make a phone call from Android app

In this android programming source code example, we are going to make a phone call from Android app.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to make a phone call from Android app.

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"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity2">

    <EditText
        android:id="@+id/pnEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:hint="Phone Number"
        android:textColor="#000000"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/callBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:text="Call"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/pnEdt"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

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 com.bluapp.androidview2.R;

public class DeviceFeaturesActivity2 extends AppCompatActivity {
    private EditText pnEdt;
    private Button callBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features2);
        pnEdt = (EditText) findViewById(R.id.pnEdt);
        callBtn = (Button) findViewById(R.id.callBtn);

        callBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phonenumber = pnEdt.getText().toString();
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+phonenumber));
                startActivity(intent);
            }
        });
    }
}

AndroidManifest.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment