Adding the Password Dialog to the Quiz SettingsActivity Class

To create a custom AlertDialog, you must first declare a unique identifier to represent the dialog, as follows: static final int PASSWORD_DIALOG_ID = 1;

Next, you need to update the onCreateDialog() method of QuizSettingsActivity to include a case statement for the new Dialog identifier:

case PASSWORD_DIALOG_ID: // Build Dialog // Return Dialog

Now let's look at how to build the password dialog from the ground up. You begin by inflating (loading) the layout you created into a View control:

LayoutInflater inflater =

(LayoutInflater) getSystemService(Context. LAYOUT_INFLATER_SERVICE); final View layout =

inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));

To load the password_dialog.xml layout file into a view, you must retrieve LayoutInflater and then call the inflate() method, passing in the layout resource identifier as well as the root layout control's identifier (in this case, the LinearLayout encapsulating the Dialog controls, called root).

Once a layout has been inflated into a View, it can be modified programmatically much like a regular layout. At this point, controls can be populated with data, and event listeners can be registered.

For example, to retrieve the EditText_Pwd1 control from the layout view, you call the findViewById() method, as follows:

final EditText pi =

(EditText) layout.findViewById(R.id.EditText_Pwd1); final EditText p2 =

(EditText) layout.findViewById(R.id.EditText_Pwd2);

At this point, you register any event listeners on the EditText fields, such as those discussed earlier to watch EditText input and match the strings as the user types.

For example, you can register to listen for text change events in the second EditText password field using a TextWatcher and match the contents of the field to that in the first EditText field. You can then display the password matching status in the third TextView control we created called TextView_PwdProblem:

final TextView error =

(TextView) layout.findViewById(R.id.TextView_PwdProblem); p2.addTextChangedListener(new TextWatcher() { ©Override public void afterTextChanged(Editable s) {

String strPassl = p1.getText().toString(); String strPass2 = p2.getText().toString(); if (strPass1.equals(strPass2)) {

error.setText(R.string.settings_pwd_equal); } else {

error.setText(R.string.settings_pwd_not_equal);

// ... other required overrides do nothing

The TextWatcher has a number of methods which require implementation. However, the one we're must interested in is the afterTextChanged() method.

Now that you have inflated the view and configured it for use, you can to attach it to AlertDialog. To do this, you use the AlertDialog.Builder class:

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(layout);

builder.setTitle(R.string.settings_button_pwd);

First, you set the view of AlertDialog.Builder to the layout you inflated. Then you set the title of the Dialog window with the setTitle() method.

Your dialog will have two Button controls: a positive button (OK) and a negative button (Cancel). Because you do not want this dialog cached for reuse by the activity, both Button handlers need to call the removeDialog() method, which destroys the dialog:

QuizSettingsActivity.this

.removeDialog(PASSWORD_DIALOG_ID);

The positive button (OK) requires some handling. When the user clicks the positive button, you need to extract the password text from the EditText controls, compare it, and, if two strings match, store the new password:

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

public void onClick(DialogInterface dialog, int which) { TextView passwordlnfo =

(TextView) findViewById(R.id.TextView_Password_Info); String strPasswordl = p1.getText().toString(); String strPassword2 = p2.getText().toString(); if (strPassword1.equals(strPassword2)) { Editor editor = mGameSettings,edit(); editor.putString( GAME_PREFERENCES_PASSWORD, strPasswordl); editor.commit();

passwordInfo.setText(R.string.settings_pwd_set); } else {

Log.d(DEBUG_TAG, "Passwords do not match. "

+ "Not saving. Keeping old password (if set).");

QuizSettingsActivity.this

.removeDialog(PASSWORD_DIALOG_ID);

The negative button, Cancel, simply returns the user to the main screen. You configure the negative button using the setNegativeButton() method of the builder:

builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) { QuizSettingsActivity.this

.removeDialog(PASSWORD_DIALOG_ID);

When your dialog is fully configured using the builder, you call the create() method to generate the custom AlertDialog and return it:

AlertDialog passwordDialog = builder.create(); return passwordDialog;

0 0

Post a comment