How To Call Separate Person In Phone Dialer For Android Application Using Android Studio

Introduction

 
This article helps to describe how to call separate person in phone dialer for an Android Application, using Android Studio.
 
Requirements
 
Android Studio version 2.1.3
          
If you develop phone dialer Application, you should follow the steps given below.
 
Step 1
 
Open Android Studio, choose the File and select the New. Afterwards, click New Project. Here, you will write the Application name, select the Next, select the Target version, choose the Activity and write the activity name. Afterwards, click Finish.
 
Step 2
 
Here, you will go to activity_main.xml and build the design, which is like options button Textview .
 
 
Graphical User Interface design.
 
 
Step 3
 
Now, you will build XML code in Activity_main.xml code. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="xyz.rvconstructions.www.phonedialerapp.MainActivity">  
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center"  
  15.         android:layout_marginBottom="25dip"  
  16.         android:text="@string/main_label"  
  17.         android:textColor="@color/colorPrimary"  
  18.         android:textSize="22sp"  
  19.         android:id="@+id/textView" />  
  20.   
  21.     <Button  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="New Button"  
  25.         android:id="@+id/button"  
  26.         android:layout_below="@+id/textView"  
  27.         android:layout_centerHorizontal="true" />  
  28.   
  29.     <Button  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:text="New Button"  
  33.         android:id="@+id/button2"  
  34.         android:layout_below="@+id/button"  
  35.         android:layout_alignLeft="@+id/button"  
  36.         android:layout_alignStart="@+id/button" />  
  37.   
  38.     <Button  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="New Button"  
  42.         android:id="@+id/button3"  
  43.         android:layout_below="@+id/button2"  
  44.         android:layout_centerHorizontal="true" />  
  45.   
  46.     <Button  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:text="New Button"  
  50.         android:id="@+id/button4"  
  51.         android:layout_below="@+id/button3"  
  52.         android:layout_alignLeft="@+id/button3"  
  53.         android:layout_alignStart="@+id/button3" />  
  54.   
  55.     <Button  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:text="New Button"  
  59.         android:id="@+id/button5"  
  60.         android:layout_below="@+id/button4"  
  61.         android:layout_alignLeft="@+id/button4"  
  62.         android:layout_alignStart="@+id/button4" />  
  63.   
  64.     <Button  
  65.         android:layout_width="wrap_content"  
  66.         android:layout_height="wrap_content"  
  67.         android:text="New Button"  
  68.         android:id="@+id/button6"  
  69.         android:layout_below="@+id/button5"  
  70.         android:layout_alignLeft="@+id/button5"  
  71.         android:layout_alignStart="@+id/button5" />  
  72. </RelativeLayout>  
  73. //xml string  
  74. <resources>  
  75.     <string name="app_name">PhoneDialerApp</string>  
  76.     <string name="main_label">My Friends</string>  
  77. </resources>  
Step 4
 
Now, you will go to MainActivity.java code and build Java code. First of all, you would declare the header file.
 
 
Step 5
 
Here, you will declare the local variable.

 
Step 6
 
Here, you will declare the set up buttons and attach click listeners.
 
 
Step 6
 
Here, you will declare the launchDialer() method.
 
 
Step 7
 
Now, you need to declare the populateArrays() method.
 
 
 
Step 8
 
Here, you will declare onClick() method.
 
 
Step 9
 
Now, you will see the full MainActivity.java code. 
  1. package xyz.rvconstructions.www.phonedialerapp;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.content.Intent;  
  6. import android.net.Uri;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends AppCompatActivity implements OnClickListener{  
  12.   
  13.     private int entr = 6;  
  14.     private String phoneNum[];  
  15.     private String buttonLabels[];  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         phoneNum = new String[entr];  
  22.         buttonLabels = new String[entr];  
  23.         populateArrays();  
  24.         Button button1 = (Button)findViewById(R.id.button);  
  25.         button1.setText(buttonLabels[0]);  
  26.         button1.setOnClickListener(this);  
  27.   
  28.         Button button2 = (Button)findViewById(R.id.button2);  
  29.         button2.setText(buttonLabels[1]);  
  30.         button2.setOnClickListener(this);  
  31.   
  32.         Button button3 = (Button)findViewById(R.id.button3);  
  33.         button3.setText(buttonLabels[2]);  
  34.         button3.setOnClickListener(this);  
  35.   
  36.         Button button4 = (Button)findViewById(R.id.button4);  
  37.         button4.setText(buttonLabels[3]);  
  38.         button4.setOnClickListener(this);  
  39.   
  40.         Button button5 = (Button)findViewById(R.id.button5);  
  41.         button5.setText(buttonLabels[4]);  
  42.         button5.setOnClickListener(this);  
  43.   
  44.         Button button6 = (Button)findViewById(R.id.button6);  
  45.         button6.setText(buttonLabels[5]);  
  46.         button6.setOnClickListener(this);  
  47.     }  
  48.   
  49.     public void launchDialer(String number){  
  50.         String numberToDial = "tel:"+number;  
  51.         startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));  
  52.     }  
  53.     public void populateArrays(){  
  54.         phoneNum[0] = "9159490774";  
  55.         phoneNum[1] = "8754906484";  
  56.         phoneNum[2] = "9944085454";  
  57.         phoneNum[3] = "9988776655";  
  58.         phoneNum[4] = "9988776644";  
  59.         phoneNum[5] = "9988776633";  
  60.   
  61.         buttonLabels[0] = "Muthuram";  
  62.         buttonLabels[1] = "Ram";  
  63.         buttonLabels[2] = "Santhosh";  
  64.         buttonLabels[3] = "Saran";  
  65.         buttonLabels[4] = "Harish";  
  66.         buttonLabels[5] = "prasath";  
  67.     }  
  68.   
  69.     @Override  
  70.     public void onClick(View v) {  
  71.         switch (v.getId()) {  
  72.   
  73.             case R.id.button:  
  74.                 launchDialer(phoneNum[0]);  
  75.                 break;  
  76.   
  77.             case R.id.button2:  
  78.                 launchDialer(phoneNum[1]);  
  79.                 break;  
  80.   
  81.             case R.id.button3:  
  82.                 launchDialer(phoneNum[2]);  
  83.                 break;  
  84.   
  85.             case R.id.button4:  
  86.                 launchDialer(phoneNum[3]);  
  87.                 break;  
  88.   
  89.             case R.id.button5:  
  90.                 launchDialer(phoneNum[4]);  
  91.                 break;  
  92.   
  93.             case R.id.button6:  
  94.                 launchDialer(phoneNum[5]);  
  95.                 break;  
  96.         }  
  97.     }  
  98. }  
Step 10
 
Now, run the Application. After few minutes, you would be able to see the given output.
 
 
Here, you will select the one person. Afterwards, you would call to that person.
 
 
Here, you will choose any one action.
 
 
Now, you would call to that person.
 
 

Summary

 
I hope, this article helped you on how to call separate person via a phone dialer in an Android Application, using Android Studio.


Similar Articles