Handling Button Presses

The Button controls on the game screen are used to drive the ImageSwitcher and TextSwitcher controls. Each time the user clicks a Button control, any score changes are logged, and the ViewSwitcher controls are updated to display the next question. In this way, the Button controls drive the activity forward, and the user progresses through the trivia quiz questions.

There is little difference between the handling of the Yes and No Button controls. Let's take a closer look at the OnClickListener.OnClick() method of the Yes Button control:

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


The View.onClickListener() method for the corresponding No Button is almost identical to the Yes Button control handler shown here. The only difference is that you pass a false value into the handleAnswerAndShowNextQuestion() method. This is a custom method you will implement to log the score change and handle any ViewSwitcher logic.

Now let's look in more detail at the handleAnswerAndShowNextQuestion() method, which takes one parameter, boolean bAnswer. First, examine the following pseudocode showing what this Button handler will do:

private void handleAnswerAndShowNextQuestion(boolean bAnswer) { // Load game settings like score and current question // Update score if answer is "yes"

// Load the next question, handling if there are no more questions

Now let's work through the pseudo-code and implement this method. First, you must retrieve the current game settings, including the game score and the next question number, from SharedPreferences:

int curScore =

mGameSettings,getInt(GAME_PREFERENCES_SCORE, 0); int nextQuestionNumber =


Next, you need to increment the value of the next question in SharedPreferences: Editor editor = mGameSettings.edit();

editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, nextQuestionNumber);

If the user clicked the Yes button, you also need to update the score and save it to SharedPreferences:

editor.putInt(GAME_PREFERENCES_SCORE, curScore + 1);

After you have changed all the SharedPreferences values necessary, you save the changes with the commit() method of the editor:


Next, you check whether the next question is available in the hashtable. If you need to retrieve a new batch of questions, do so now:

if (mQuestions.containsKey(nextQuestionNumber) == false) { // Load next batch try {

loadQuestionBatch(nextQuestionNumber); } catch (Exception e) {

Log.e(DEBUG_TAG, "Loading updated question batch failed", e);

Finally, you update the TextSwitcher and the ImageSwitcher controls with the text and image for the next question:

if (mQuestions.containsKey(nextQuestionNumber) == true) { // Update question text TextSwitcher questionTextSwitcher =

(TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText); questionTextSwitcher.setText(getQuestionText(nextQuestionNumber));

// Update question image ImageSwitcher questionImageSwitcher =

(ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage); Drawable image = getQuestionImageDrawable(nextQuestionNumber); questionImageSwitcher.setImageDrawable(image); } else {


When you run the application and launch the game screen, it should look something like Figure 12.4.


The Been There, Done That! game screen.

There are two easy ways to "reset" the quiz for testing purposes. The first method is to delete the application's SharedPreferences file from the Android file system and restart the emulator. You use the Eclipse DDMS perspective to navigate to the data directory of the application and delete the associated SharedPreferences file. You can also uninstall and reinstall the application by using the Settings Menu item from the Android Home screen. Adding a reset mechanism to the Been There, Done That! application is left as an exercise to the reader at the end of this hour.

Did you Know?

0 0

Post a comment