Android Studio - how to make phone call

Hello Everyone,

Today, we are going to see how to make phone calls from your android application. Just an EditText and a Button. In the EditText you can give any phone number and click the Button, it will make the call to that number.

Let's see,

Output





Just copy and paste the below code, run it and enjoy it

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.example.dhevendhiran.phonecall.MainActivity">


    <Button
        android:id="@+id/buttonCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="100dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="8dp"
        android:text="call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/editTextNumber"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_marginBottom="300dp"
        android:layout_marginEnd="100dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Enter Phone Number"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.6" />
</android.support.constraint.ConstraintLayout>



MainActivity.java


package com.example.dhevendhiran.phonecall;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
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 static final int REQUEST_CALL = 1;
    EditText editText;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        button = (Button) findViewById( R.id.buttonCall );
        editText = (EditText) findViewById( R.id.editTextNumber );

        // when click the call button, the following onClick method will be executed
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // calling makePhoneCall method
                makePhoneCall();
            }
        } );

    }

    // make phone call method definition
    private void makePhoneCall() {

        // get the number from user using edtitext
        String number = editText.getText().toString();

        // to check the edittext contains any numbers or if the user enter the number. If the user do not enter eny number, the else part will be executed
        if(number.trim().length() > 0){

            // to check the permissions to access phone default calling activity
            if(ContextCompat.checkSelfPermission( MainActivity.this, Manifest.permission.CALL_PHONE ) != getPackageManager().PERMISSION_GRANTED){
                ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL );
            }else{

                // creating a string to call. because the string must be in the following format : "tel: 9865755519"
                String dial = "tel:"+number;

                // launch the intent to make the call
                startActivity( new Intent(Intent.ACTION_CALL, Uri.parse( dial )) );
            }
        }else{
            Toast.makeText( MainActivity.this, "Enter Phone Number", Toast.LENGTH_SHORT ).show();
        }
    }
}



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dhevendhiran.phonecall">

    <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>

That's it..
Thank You for reading
Hope this will help

If any doubt, please don't hesitate to ask

Comments

Post a Comment

Popular posts from this blog

Seating Arrangement Problem - HackerEarth