Enabling Friend Requests on the Settings Screen

To add friend support to the Been There, Done That! application, you need to update QuizSettingsActivity to allow the user to input friend email addresses. Specifically, you need to do the following:

► Add a button to the settings screen to launch a new dialog.

► Implement the dialog to QuizSettingsActivity to allow the user to input a friend's email address.

► Add some networking code to communicate the friend request to the application server.

Updating the Settings Screen Layout

You need to update the user interface of the Been There, Done That! application to allow a player to enter friends' email addresses. There are a number of ways you could go about doing this, of course. You could add a new activity and update the menu screen, allowing for a whole new screen in the application, or you could just update the settings screen with a new area.

To keep things simple, you can just add a new section at the bottom of the settings screen that acts much like the other settings that rely on a dialog (see Figure 17.1).

screen updated to allow for friend requests.


The settings

I Female

For example, you could add the following just below the favorite place layout controls:

<TextView android:id= "@+id/TextView_Friend_Email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/settings_friend_email_label" android:textSize="@dimen/help_text_size" android:textStyle="bold"></TextView> <LinearLayout android:id="@+id/LinearLayout_Friend_Email" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:id="@+id/Button_Friend_Email" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/settings_button_friend_email"></Button> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="@dimen/help_text_size" android:textStyle="bold" android:gravity="center'

android:id="@+id/TextView_Friend_Email_Tip" android:text="@string/settings_friend_email_tip"></TextView> </LinearLayout>

Like other settings on this screen, the layout updates involve adding several TextView labels and a Button control called Button_Friend_Email. Clicking this button will launch a new dialog. Therefore, you need to add a new layout resource to describe the dialog layout (see Figure 17.2).

Editing config: default

[Explode [Outline] [|] ]

Devices) ADP1 ▼Config


Create... J

& Layouts 3 AbsoluteLayout ©DialerFilter tH ExpandableListView CSFrameLayout ©GridView El HorizontalScrollView

Enter email address of friend:


& Views © GestureOverlayView ©SurfaceView


®View ® ViewStub ® AnalogClock ® AutoCompleteTextView

Layout | frierd_entiyj<ml


Preview of the friend request dialog layout.

This layout should be defined as follows in the XML layout file called / res/ layout/friend_entry.xml:

<?xml version="i.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk7res/android"






<TextView android:id= "@+id/TextView_Friend_Email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/help_text_size" android:textStyle="bold"

android:text="@string/settings_friend_email"></TextView> <EditText android:id="@+id/EditText_Friend_Email" android:layout_height="wrap_content" android:maxLength="50" android:layout_width="fill_parent" android:maxLines="i"

android:inputType="textEmailAddress"></EditText> </LinearLayout>

The contents of this layout are straightforward. The layout is a LinearLayout container with two controls: a TextView label that prompts the user to enter an email address and an EditText control to receive the email address string from the user.

Launching the Friend Request Dialog

Clicking the Button control called Button_Friend_Email launches a dialog that allows the user to enter a friend's email address. This dialog is launched much the same as any other dialog in the settings screen:

Button addFriend = (Button) findViewById(R.id.Button_Friend_Email); addFriend.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {


You need to update the onCreateDialog() method of the QuizSettingsActivity class to include a case statement for this new dialog: case FRIEND_EMAIL_DIALOG_ID:

final View friendDialogLayout = layoutInflater.inflate(

R.layout.friend_entry, (ViewGroup) findViewByld(R.id.root));

AlertDialog.Builder friendDialogBuilder =

new AlertDialog.Builder(this); friendDialogBuilder.setView(friendDialogLayout); final TextView emailText = (TextView)



android.R.string.ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

String friendEmail = emailText.getText().toString(); if (friendEmail != null && friendEmail.length() > 0) { doFriendRequest(friendEmail);

return friendDialogBuilder.create();

This dialog implementation should look quite familiar. Again, you are building up an AlertDialog control by inflating a layout resource. When the user clicks the OK button in the dialog (see Figure 17.3), the email address is communicated to the application server asynchronously, using the FriendRequestTask class. (We'll talk more about this in a moment.) The application server is responsible for setting up the friend relationship if the friend's email address exists in the datastore.


The friend request dialog.

Creating an AsyncTask Subclass to Handle Friend Requests

To send friend requests to the application server, you need to add another AsyncTask subclass called FriendRequestTask to QuizSettingsActivity. The FriendRequestTask class can be run using the execute() method whenever the player inputs a friend's email address in the friend request dialog and presses OK.

The details of the FriendRequestTask class are very similar to those of the other AsyncTask subclasses you have written over the past few hours. (In the following example, exception handling has been removed for clarity and brevity.) The only really interesting part to this new task is the friend request code within the doInBackground() method: ©Override protected Boolean doInBackground(String... params) { Boolean succeeded = false; String friendEmail = params[0]; SharedPreferences prefs =

getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE); Integer playerId = prefs.getInt(GAME_PREFERENCES_PLAYER_ID, -1);

Vector<NameValuePair> vars = new Vector<NameValuePair>(); vars.add(new BasicNameValuePair("command", "add")); vars.add(new BasicNameValuePair("playerId", playerId.toString())); vars.add(new BasicNameValuePair("friend", friendEmail)); HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(TRIVIA_SERVER_FRIEND_EDIT); request.setEntity(new UrlEncodedFormEntity(vars));

ResponseHandler<String> responseHandler = new BasicResponseHandler();

String responseBody = client.execute(request, responseHandler); if (responseBody != null) { succeeded = true;

return succeeded;

Here, you use HttpClient to execute an HTTP POST request, using form variables, to the application server. You can form the HttpPost request object by combining the appropriate application server URL with the query variables, encoded using the handy UrlEncodedFormEntity class, set as the entity for the POST request. When the query is fully formed, you can execute the request by using the execute() method of HttpClient, as you have in previous examples. Then you simply check the response to determine whether the friend request was successful by checking the resulting ResponseHandler object.

Was this article helpful?

0 0

Post a comment