Rhisl Modify the Android PhoneDialer Project

If you played around with the latest version of the AndroidPhoneDialer, you may have noticed something missing. Unfortunately, the way the project is currently written, it allows you to input any type of value into the EditText View and try to send it to the Call Activity. This is really not an optimal approach to application development.

Do some research and add some validation to the EditText. Use the following parameters to modify your project:

• Use a regular expression to validate that a phone number was entered in the EditText (package java.regex).

• Use the showAlert( ) syntax to display a message telling the user they input something that does not match your regular expression.

When you feel you have a working solution, compare it against the following code. main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:id="@+id/textLabel"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Enter Number to Dial:"

<EditText android:id="@+id/phoneNumber" android:layout_width="fill_parent"

android:layout_height="wrap_content"

<Button android:id="@+id/callButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Show Dialer" />

</LinearLayout>

AndroidPhoneDialer.java package android_programmers_guide.AndroidPhoneDialer;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

import android.content.Intent;

import android.net.Uri;

import android.widget.EditText;

import java.util.regex.*;

public class AndroidPhoneDialer extends Activity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main ); final EditText phoneNumber = (EditText) findViewById(R.id.phoneNumber );

final Button callButton = (Button) findViewById(R.id.callButton); callButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

if (validatePhoneNumber(phoneNumber.getText().toString())){ Intent CallIntent = new Intent(Intent. CALL_ACTION,Uri.parse("tel:" + phoneNumber.getText()));

CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); startActivity(CallIntent);

else{

showAlert("Please enter a phone number in the X-XXX-XXX-XXXX format.",0, "Format Error", "Re-enter Number",false);

public boolean validatePhoneNumber(String number){

Pattern phoneNumber = Pattern.compile("(\\d-)?(\\d{3}-)?\\d{3}

Matcher matcher = phoneNumber.matcher(number); return matcher.matches();

When you run the project, it should produce a message similar to that in the following illustration.

In the next chapter, you will learn about more Views. You will create a multi-Activity application that will allow you to explore and create Views that have not yet been discussed in this book. You will also create and utilize a menu system that will launch your Activities.

0 0

Post a comment