Android application that triggers the phone dial screen

As you know, I am following the free course on Developing Android applications in Java by CreativeTech and O’Reilly (even you should, if you are interested in developing apps for android) and this week’s homework in the course is to create an app which will show the phone dial screen when a button is clicked.

I just finished it and I thought of posting the source code and explanation so that it will be useful for others too.

Creating the project

The first step is to create the android project. You should follow the instructions given in the android documentation. You would need Eclipse and the android SDK to be installed to do this.

You will have an empty project to start with and with the default project structure, which I explained in my previous post.

Adding the button to the view

If you have followed the first session then you know that adding a button to the view is quite easy. All you have to do is to open your view file (/res/layout/main.xml) and add the following code.

<Button
    android:id="@+id/dialer_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/dialer"
/>

You should also declare the value for the @string/dialer key in your values (/res/values/strings.xml) file by adding the following line

<string name="dialer">Phone Dialer</string>

Adding the button to the activity

After adding the button to the view, we have to declare the button in the activity. To do so, we have to add the following line in the Activity file.

Button dialerButton = (Button)findViewById(R.id.dialer_button);

Bind the Listener to the button

After creating the button instance in the activity, we have to bind a click listener to it. In the click listener we have to invoke the Phone dialer intent. Add the following code to the activity

dialerButton.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
		//open the phone dialer on clicking the button
		Intent intent = new Intent(Intent.ACTION_DIAL);
		startActivity(intent);
	}
});

In the above snippet the following line does the trick

Intent intent = new Intent(Intent.ACTION_DIAL);

It creates the intent, which will open the phone dialer.

Finishing up

So that’s it, you are done 🙂 All you have to do now is to save the project and run it in the emulator. If everything is done properly, you will see the following screen in the emulator.

android-homework-1  android-homework-2

When you click the button, it should open the phone dialer.

Source code

I have uploaded the entire project source code into github and you download it from there and verify it with your code.

Try to complete the homework, before the next session and do come back to view the notes and the homework for the next session too. 🙂

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Related posts

Tags: , ,

3 Comments so far

Follow up comments through RSS Feed | Post a comment

1 Tweetbacks so far

3 Trackbacks/Pingbacks so far

Leave a Reply

Your email address will not be published. Required fields are marked *