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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
        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>  

    chevron_right

    
    

  • 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <!--Relative Layout-->
    <RelativeLayout
        xmlns:androclass="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">
       <!--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>

    chevron_right

    
    

  • 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



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
                }
            });
        }
    }

    chevron_right

    
    

Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.