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
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
package
=
"com.geeksforgeeks.phonecall"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"8"
android:targetSdkVersion
=
"16"
/>
<!--permission for phone call-->
<
uses-permission
android:name
=
"android.permission.CALL_PHONE"
/>
<
application
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/gfg"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
"com.geeksforgeeks.phonecall.MainActivity"
android:label
=
"@string/gfg"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android: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
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!--Relative Layout-->
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<!--Edit text for phone number-->
<
EditText
android: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-->
<
Button
android: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
package
com.geeksforgeeks.phonecall;
import
android.os.Bundle;
import
android.support.v7.app.AppCompatActivity;
import
android.content.Intent;
import
android.widget.EditText;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.net.Uri;
import
android.widget.Button;
public
class
MainActivity
extends
AppCompatActivity {
// define objects for edit text and button
EditText edittext;
Button button;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting instance of edittext and button
button = findViewById(R.id.button);
edittext = findViewById(R.id.editText);
// Attach set on click listener to the button
// for initiating intent
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg)
{
// getting phone number from edit text
// and changing it to String
String phone_number
= edittext.getText().toString();
// Getting instance of Intent
// with action as ACTION_CALL
Intent phone_intent
=
new
Intent(Intent.ACTION_CALL);
// Set data of Intent through Uri
// by parsing phone number
phone_intent
.setData(Uri.parse(
"tel:"
+ phone_number));
// start Intent
startActivity(phone_intent);
}
});
}
}
Output:
Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!