5.6.7. Method loadNextFlag

Method loadNextFlag (Fig. 5.31) loads and displays the next flag and the corresponding set of answer Buttons. The image file names in quizCountriesList have the format

regionName-countryName

without the .png extension. If a regionName or countryName contains multiple words, they’re separated by underscores (_).

Line 184 removes the first name from quizCountriesList and stores it in nextImage. We also save this in correctAnswer so it can be used later to determine whether the user made a correct guess. Next, we clear the answerTextView and display the current question number in the questionNumberTextView (lines 189–191) using the formatted String resource R.string.question.

Line 194 extracts from nextImage the region to be used as the assets subfolder name from which we’ll load the image. Next we get the AssetManager, then use it in the try statement to open an InputStream (package java.io) to read bytes from the flag image’s file. We use that stream as an argument to class Drawable’s static method createFromStream, which creates a Drawable object (package android.graphics.drawable). The Drawable is set as flagImageView’s item to display by calling its setImageDrawable method. If an exception occurs, we log it for debugging purposes (line 211).

Next, line 214 shuffles the fileNameList, and lines 217–218 locate the correctAnswer and move it to the end of the fileNameList—later we’ll insert this answer randomly into the one of the guess Buttons.

Lines 221–236 iterate through the Buttons in the guessLinearLayouts for the current number of guessRows. For each Button:

• lines 228–229 get a reference to the next Button

• line 230 enables the Button

• line 233 gets the flag file name from the fileNameList

• line 234 sets Button’s text with the country name that’s returned by method getCountryName (Section 5.6.8)

Lines 239–243 pick a random row (based on the current number of guessRows) and column, then set the text of the corresponding Button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset