Simple Dialer Application

Welcome back, in this post we are going to develop a simple dialer application where you can manually enter the phone number and make a call. You may think that I already have default dialer application installed in my mobile but here we are just going to understand how it works. Feels great?. :)

Quick Links

  1. Project Structure
  2. Code Listings
  3. Demo
  4. Download Source Code

Project Structure

Launch eclipse and create a Android project with a project name “DialerApp”.

Code Listings

Layout creation:

Here we are going to have three controls: TextView to display the name of the application say ‘Dialer Application’, EditView to enter the phone number and Button to make a call. Let us see the properties of each control:

TextView Control:

Assign android:layout_width with value ‘match_parent’ and android:layout_height property with value ‘wrap_content’
Make the text visible at the center of the label so assign android:gravity with value ‘center’
Assign android:text property with value ‘Dialer Application’
Want text little bit bolder than usual assign android:textAppearance with value “?android:attr/textAppearanceLarge”

 EditView Control:

Assign android:layout_width with value ‘match_parent’ and android:layout_height property with value ‘wrap_content’
Set android:ems=”10″ which sets the text size to that ems value
Assign android:inputType=”phone” so textview accepts only numbers and other dial characters like *, #
Limit the size to 11, since phone number length in India is 11 including leading ‘0’.

 Button Control:

Assign android:layout_width with value ‘”93dp” and android:layout_height property with value ‘wrap_content’
Set android:layout_gravity=”center” which displays the button at the center
Assign android:gravity=”center” which centers the text
Assign android:text = “Call”

Final layout xml and the layout design:

<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” >

<TextView android:id=”@+id/textView1″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center” android:text=”Dialer Application” android:textAppearance=”?android:attr/textAppearanceLarge” />

<EditText android:id=”@+id/editText1″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:ems=”10″ android:inputType=”phone” android:maxLength=”11″ >

<requestFocus /> </EditText>

<Button android:id=”@+id/button1″ android:layout_width=”93dp” android:layout_height=”wrap_content” android:layout_gravity=”center” android:gravity=”center” android:text=”Call” />

</LinearLayout>

Create two instance objects in DialerAppActivity class, one for referring the textview we created and other for referring button to make a call when it is clicked.

Button dialBtn;//To refer the button control
EditText numTxt;//To refer the edit view control

Initialize the objects which we created in onCreate() method . The onCreate() method will be called when we launch application.

//Refer the button with the help of button id
dialBtn = (Button) findViewById(R.id.button1);
//Refer the edit view control with the help of its id
numTxt = (EditText) findViewById(R.id.editText1);

Add listener for button in onCreate() method

dialBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
if (numTxt != null && (numTxt.getText().length()==10
                          ||numTxt.getText().length()==11)) {
startActivity(new Intent(Intent.ACTION_CALL, 
                       Uri.parse("tel:" + numTxt.getText())));
}else if(numTxt != null && numTxt.getText().length()==0){
Toast.makeText(getApplicationContext(), 
      "You missed to type the number!", Toast.LENGTH_SHORT).show();
}else if(numTxt != null && 
                        numTxt.getText().length()<10){
Toast.makeText(getApplicationContext(),"Check whether you 
entered correct number!",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("DialerAppActivity", "error: " + 
e.getMessage(),e);//Runtime error will be logged
}
}
});

[pgsubscribe]

Detailed explanation of listener code is here:

Scenario 1: When the textview has value and its length is equal to 10 or 11, we will start an another activity called system ‘call’ activity. You have to pass the number prefixed with ‘tel’ and it should be parsed with parse() method present in Uri class.

Scenario 2: When the textview value’s length is equal to zero, we will notify the user with the Toast message saying that “You missed to type the number!”.

Scenario 3: When the textview value’s length is lesser than 10, we will notify the user with the Toast message saying that “Check whether you entered correct number!”.

[pglinkadssmall1]

Want to know more about intent?

A Toast is a message that pops up on the surface of the window.

By default we don’t have privilege to invoke call activity, so explicitly we must add the permission in manifest.xml file.

Add <uses-permission android:name=”android.permission.CALL_PHONE” /> under <uses-sdk />tag in manifest.xml which is present right under the project folder. Don’t forget to add permission!.

Demo

Everything is done, we are gonna test the code.

Run the application just by right clicking on the project, select the configured emulator or device in case if you connected one.

Application will look like:


Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

 

[pglinkadssmall]

I hope you have enjoyed the post.. :)

Suggested posts for further reading


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
  • Ragesh

    very good work…!

  • vignesh

    very very thanks man… each and every single line you gave definition.. thanks a lot and keep it up and we want many different program like tiz… :)

  • srinivasan

    I’ve done as you said. But it’s not working. Can’t get final result.

    • Ganesh

      Download the source code and give a try or post the error you are getting here.

  • Vishal

    That’s good but post a tutorial that shows a custom dialer pad when i launch my application, and that acts same like our android default dialer pad acts…..thanks in advance.

  • sara

    Thank you So MUCH

    I want to see the code of the second page which shows the timer and end call buttons !!

    Thank you So much

    waiting for replay

    • Second page you are referring here is device default caller app whose source can be found inside Android OS source code. You need to download the Android OS source code I mean all packages in order to see the source code of caller app.

  • Nazeem

    Thank You

  • KaBoZ

    Thnx for the tutorial :) However the links do not work, can you pls re-upload the apk and source code ?
    Much apreciated

  • Its fixed. Try now.

  • abdou

    hay every one !!
    i have a question ; about what method can tell us that the dicussion is begin or the phone is picket up ???,
    thanks in advance .

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares