Note

You do not have to set the android:text attribute because you do not need any default text.

The id is set to phoneNumber, which is the name you will use to refer to the EditText View in the code. Again, there should be no surprises when setting up main.xml. Your final file should look like this:

<?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>

The main.xml file is now completed. You can move on to AndroidPhoneDialer.java. If you are not using an existing version of AndroidPhoneDialer.java—one from a previous project in this chapter—you may want to refer to the previous sections to see what code is added to the .java file. This will ensure that you start from the correct point in the code.

The first item you need to add to your .java file is the package definition. You need to add packages not only for the Uri, Button, and Intent, but also for the EditText:

import android.widget.Button; import android.content.Intent; import android.net.Uri; import android.widget.EditText;

The syntax to set up your EditText View is the same as that for the Button:

final EditText <edittext_name> = <edittext>

Again, call your EditText phoneNumber. The code to create your EditText is as follows:

final EditText phoneNumber = (EditText) findViewById(R.id.phoneNumber);

Once your phoneNumber EditText is created, you can use it to reference the text that is input on the device. All you have to do now is call phoneNumber.getText( ) to retrieve the user's input. Replace the hard-coded value "tel:5551212" in the following line,

Intent(Intent.CALL_ACTION,Uri. parse("tel:5551212")); with the value of getText( ):

Intent(Intent.CALL_ACTION,Uri.parse("tel:" + phoneNumber.getText()));

That is all the new code you need to update your project. With these simple two additions, you can give the user an object with which to input a phone number, and send that number to the phone's Call Activity. The full code in the .java file should look like this:

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;

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){ Intent CallIntent = new Intent(Intent.CALL_ACTION,Uri.parse("tel:" + phoneNumber.getText())); CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); startActivity(CallIntent);

When you run the application in your Emulator, you should see a screen that resembles the following illustration.

0 0

Post a comment