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.
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.
The homework seems pretty straight forward if we understand the flashlight app.
One question, did you go through all the features available in the Intent class by just looking at the java docs or did you also refer something else to get a complete picture?
I just went through the JavaDocs alone.
If you like dialers and curious about dialers so you must check “My Sales Dialer”. this dialer is really different from other phone dialers as this is call center dialer in mobile. Available FREE on android market. This dialer is same like call center power dialer in phone… Necessary for professional callers…
https://market.android.com/details?id=com.cbsinfosys.MySalesDialer&hl=en