Make call in Android programmatically with example


            Its quite simple to make call in Android programmatically, it is just a few lines of code. The following tutorial explains this with an example and source code for your reference.

           This example has an editText and Button in the UI, editText is set to inputType="phone" that accepts only numbers and button click will open the dialer with the number entered.
            To open the dialer we need to use Intent and the action to be performed here is ACTION_DIAL, and data to be provided is the phone number, which initiates call explicitly. Below is the code snippet that performs this operation.
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phnum)); 

phnum is the number to be dialed here got throught editText component. Finally, we should start the activity as shown below :
startActivity(callIntent);

Android permission to make call
              This permission statement needs to be added to AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />

Thats it, these steps are enough to make call in android programmatically.





activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="47dp"
        android:ems="10"
        android:inputType="phone" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttonCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="DIAL" />


</RelativeLayout>

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

private Button button;
private EditText etPhoneno;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
etPhoneno = (EditText) findViewById(R.id.editText1);
// add button listener
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
String phnum = etPhoneno.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phnum));
startActivity(callIntent);
}
});
}

}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.makephonecall_android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


</manifest>

Source code of this application
MakeCallAndroid.zip



Reactions:

2 comments :

  1. hmm nice admin but how can i call with the contact name

    ReplyDelete