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.
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.
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="xyz.rvconstructions.www.phonedialerapp.MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginBottom="25dip"
- android:text="@string/main_label"
- android:textColor="@color/colorPrimary"
- android:textSize="22sp"
- android:id="@+id/textView" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button"
- android:layout_below="@+id/textView"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button2"
- android:layout_below="@+id/button"
- android:layout_alignLeft="@+id/button"
- android:layout_alignStart="@+id/button" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button3"
- android:layout_below="@+id/button2"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button4"
- android:layout_below="@+id/button3"
- android:layout_alignLeft="@+id/button3"
- android:layout_alignStart="@+id/button3" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button5"
- android:layout_below="@+id/button4"
- android:layout_alignLeft="@+id/button4"
- android:layout_alignStart="@+id/button4" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Button"
- android:id="@+id/button6"
- android:layout_below="@+id/button5"
- android:layout_alignLeft="@+id/button5"
- android:layout_alignStart="@+id/button5" />
- </RelativeLayout>
- //xml string
- <resources>
- <string name="app_name">PhoneDialerApp</string>
- <string name="main_label">My Friends</string>
- </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.
- package xyz.rvconstructions.www.phonedialerapp;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.content.Intent;
- import android.net.Uri;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity implements OnClickListener{
-
- private int entr = 6;
- private String phoneNum[];
- private String buttonLabels[];
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- phoneNum = new String[entr];
- buttonLabels = new String[entr];
- populateArrays();
- Button button1 = (Button)findViewById(R.id.button);
- button1.setText(buttonLabels[0]);
- button1.setOnClickListener(this);
-
- Button button2 = (Button)findViewById(R.id.button2);
- button2.setText(buttonLabels[1]);
- button2.setOnClickListener(this);
-
- Button button3 = (Button)findViewById(R.id.button3);
- button3.setText(buttonLabels[2]);
- button3.setOnClickListener(this);
-
- Button button4 = (Button)findViewById(R.id.button4);
- button4.setText(buttonLabels[3]);
- button4.setOnClickListener(this);
-
- Button button5 = (Button)findViewById(R.id.button5);
- button5.setText(buttonLabels[4]);
- button5.setOnClickListener(this);
-
- Button button6 = (Button)findViewById(R.id.button6);
- button6.setText(buttonLabels[5]);
- button6.setOnClickListener(this);
- }
-
- public void launchDialer(String number){
- String numberToDial = "tel:"+number;
- startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));
- }
- public void populateArrays(){
- phoneNum[0] = "9159490774";
- phoneNum[1] = "8754906484";
- phoneNum[2] = "9944085454";
- phoneNum[3] = "9988776655";
- phoneNum[4] = "9988776644";
- phoneNum[5] = "9988776633";
-
- buttonLabels[0] = "Muthuram";
- buttonLabels[1] = "Ram";
- buttonLabels[2] = "Santhosh";
- buttonLabels[3] = "Saran";
- buttonLabels[4] = "Harish";
- buttonLabels[5] = "prasath";
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
-
- case R.id.button:
- launchDialer(phoneNum[0]);
- break;
-
- case R.id.button2:
- launchDialer(phoneNum[1]);
- break;
-
- case R.id.button3:
- launchDialer(phoneNum[2]);
- break;
-
- case R.id.button4:
- launchDialer(phoneNum[3]);
- break;
-
- case R.id.button5:
- launchDialer(phoneNum[4]);
- break;
-
- case R.id.button6:
- launchDialer(phoneNum[5]);
- break;
- }
- }
- }
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.