Initiating A Call From Android App


Redirecting to a Dialer in an Android application is an important feature. Suppose you are making a food delevery app or a cab booking app, you might want to take users directly to Dialer on phone to make some calls.

To do that we will demonstrate in this project how to initiate a Dialer call using an Intent.


First make a simple project with two UI elements, one a button and other an EditText to take mobile number inputs.


Ddesign the UI as per your own project needs. In my case i have done like this:

activity_main.xml:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.ourcoaching.simplecall.MainActivity">


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL NOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:text="9999999999"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="phone"
android:maxLength="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.581"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />


android.support.constraint.ConstraintLayout
>


Now we will program the java code. First instatntiate the editext and button objects

editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);


Then define an onclick listener for button:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CallFunction();
}
});


Inside the CallFuntion() method, define an intent:

Intent call = new Intent(Intent.ACTION_DIAL);

Then pass the URI of the mobile number extracted from edittext into call Intent

call.setData(Uri.parse("tel:" + number));
startActivity(call);


And thats it. On running the code, when you press the button, it will automatically redirect you to dialer on the phone with the number already loaded into dialer.


Complete java file

MainActivity.java

package com.ourcoaching.simplecall;

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 {

private Button button;
private EditText editText;

@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 view) {
CallFunction();
}
});
}

private void CallFunction() {
String number=editText.getText().toString();

if (number.length()==10){
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:" + number));
startActivity(call);
}else Toast.makeText(this,"Incorrect mobile number",Toast.LENGTH_SHORT).show();
}
}


Note: if your application requires youto directly enable call, use ACTION_CALL instead of ACTION_DIAL. Also you have to simultaneously add permission for making phone calls in Android Manifest file.

 android:name="android.permission.CALL_PHONE" />


Download the code form GitHub



Related Tutorials