How to make a phone call from your Android App?
In this article, you will make a basic android application which can be used to call some number through your android application.
You can do so with the help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers and services, here use to make phone call. This application basically contain one activity with edit text to write phone number on which you want to make a call and button to call that number.
- Step 1. Permission code in Android-Manifest.xml file
You need to take permission from user for phone call and for that CALL_PHONE permission is added in manifest file.Here is code of manifest file:Android-Manifest.xml
<?xmlversion="1.0"encoding="utf-8"?>package="com.geeksforgeeks.phonecall"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="16"/><!--permission for phone call--><uses-permissionandroid:name="android.permission.CALL_PHONE"/><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/gfg"android:theme="@style/AppTheme"><activityandroid:name="com.geeksforgeeks.phonecall.MainActivity"android:label="@string/gfg"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest> - Step 2. activity_main.xml
activity_main.xml contains a Relative Layout which contains Edit text to write phone number on which you want to make phone call and button for starting intent or making call :activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><!--Relative Layout--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><!--Edit text for phone number--><EditTextandroid:id="@+id/editText"android:layout_marginTop="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"/><!--Button to make call--><Buttonandroid:id="@+id/button"android:layout_marginTop="115dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Make Call!!"android:padding="5dp"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"/></RelativeLayout> - Step 3. MainActivity.java
In Main activity Intent object is created to redirect activity to call manager and action attribute of intent is set as ACTION_CALL.Phone number input by user is parsed through Uri and that is passed as data in Intent object which is than use to call that phone number.setOnClickListener is attached to button with intent object in it to make intent with action as ACTION_CALL to make phone call.Here is complete code:MainActivity.java
packagecom.geeksforgeeks.phonecall;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.content.Intent;importandroid.widget.EditText;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.net.Uri;importandroid.widget.Button;publicclassMainActivityextendsAppCompatActivity {// define objects for edit text and buttonEditText edittext;Button button;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Getting instance of edittext and buttonbutton = findViewById(R.id.button);edittext = findViewById(R.id.editText);// Attach set on click listener to the button// for initiating intentbutton.setOnClickListener(newOnClickListener() {@OverridepublicvoidonClick(View arg){// getting phone number from edit text// and changing it to StringString phone_number= edittext.getText().toString();// Getting instance of Intent// with action as ACTION_CALLIntent phone_intent=newIntent(Intent.ACTION_CALL);// Set data of Intent through Uri// by parsing phone numberphone_intent.setData(Uri.parse("tel:"+ phone_number));// start IntentstartActivity(phone_intent);}});}}
Output:





