Making a Phone Call in Android


In this tutorials you will learn how to make a phone call in Android.






Creating a "Call" Button and an Input Field for Number 
           
             First thing that we need to do is to modify our application layout file. Go to the res\layout directory and type the following code into the activity_main.xml file: 
 <LinearLayout 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"  
   android:orientation="vertical"  
   android:background="#696969" >  
   <EditText  
     android:id="@+id/number"  
     android:inputType="number"  
     android:layout_marginTop="100dp"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:hint="@string/number_hint"  
     android:layout_gravity="center|fill_vertical"/>  
   <Button   
     android:id="@+id/call_button"  
     android:layout_width="125dp"  
     android:layout_height="wrap_content"  
     android:text="@string/text_4_button"  
     android:background="#32CD32"  
     android:layout_gravity="center"/>  
 </LinearLayout>  
         As you can see we are using two string resources in the layout file. To create them go to the res\values directory and add these two lines to the  strings.xml file:
 <string name="number_hint">Enter a number here</string>  
 <string name="text_4_button">Call</string>  

Dialing a Number 

Now we need to modify an Activity class file, type the following code into the MainActivity.java file:
 1| package phonecallexample.tuts.com;  
 2| import android.net.Uri;  
 3| import android.os.Bundle;  
 4| import android.view.View;  
 5| import android.view.View.OnClickListener;  
 6| import android.widget.Button;  
 7| import android.widget.EditText;  
 8| import android.app.Activity;  
 9| import android.content.Intent;  
10| public class MainActivity extends Activity {  
11|   private String edittext;  
12|   private EditText number;  
13|   private Button call;  
14|   @Override  
15|   protected void onCreate(Bundle savedInstanceState) {  
16|        super.onCreate(savedInstanceState);  
17|        setContentView(R.layout.activity_main);  
18|        number = (EditText) findViewById(R.id.number);  
19|        call = (Button) findViewById(R.id.call_button);  
20|        onButtonClick();  
21|   }  
22|   private void onButtonClick(){  
23|        call.setOnClickListener(new OnClickListener() {  
24|             @Override  
25|             public void onClick(View v) {  
26|                  edittext = number.getText().toString();  
27|                  Intent i = new Intent(Intent.ACTION_CALL);  
28|            i.setData(Uri.parse("tel:" + edittext));  
29|            startActivity(i);                 
30|             }  
31|        });  
32|   }  
33| }  

Code explanation:
line 26 - here we are getting the number that a user has entered;
line 27-28 - using ACTION_CALL action to perform a call to someone specified by the data. setData() method is needed to idetify an URI of a phone number to be dialed.

Adding a New Permission

Add the following line to your AndroidManifest.xml file:
 <uses-permission android:name="android.permission.CALL_PHONE" />   
       This will allow an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed.
Note:
           You can also use ACTION_DIAL  to display the phone dialer with the given number filled in. In this case there is no need to add permissions to the AndroidManifest.xml file.

DEMO
Android Phone Call Example
Android Phone Call Example

Source Code
References:
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/reference/android/Manifest.permission.html