How to call or dial a phone number from your own custom Android App? – Complete Source code

This video shows the steps to design an Android Application which can be used to call or dial any number. This video takes CALL_PHONE permission from user by defining the respective permission in the Manifest file. In the later part of this video it shows the difference between ACTION_CALL and ACTION_DIAL intents and the respective operations it performs for these intents.

We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: programmerworld1990@gmail.com

Source Code:

package com.example.mycallerapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import static android.Manifest.permission.CALL_PHONE;

public class MainActivity extends AppCompatActivity {

private EditText editText;

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

editText = findViewById(R.id.editText);
ActivityCompat.requestPermissions(this, new String[]{CALL_PHONE}, PackageManager.PERMISSION_GRANTED);
}

public void CallButton(View view) {
if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
startActivity(new Intent(Intent.ACTION_CALL, Uri.fromParts(“tel”, editText.getText().toString(), null)));
// startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts(“tel”, editText.getText().toString(), null)));
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mycallerapplication”>

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

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<EditText
android:id=”@+id/editText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”85dp”
android:layout_marginTop=”158dp”
android:ems=”10″
android:hint=”@string/type_your_number_here”
android:inputType=”number”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
android:autofillHints=”” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”154dp”
android:layout_marginTop=”77dp”
android:onClick=”CallButton”
android:text=”@string/call”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/editText” />
</androidx.constraintlayout.widget.ConstraintLayout>

<resources>
<string name=”app_name”>My CallerApplication</string>
<string name=”type_your_number_here”>Type Your Number Here …</string>
<string name=”call”>Call</string>
</resources>

2 comments

  1. Hello sir I want a application for voice assistant for visually impaired people sir plz can u make video on this. Plz sir

Leave a Reply

%d bloggers like this: