Thursday, September 02, 2010

Android - UI Sleep

I have lately (since I got my HTC Desire phone) been tinkering a little with developing a small application for Android. Since my daughter is learning the multiplication tables and since she finds my phone "cool" then making this little app would be a good starting point.

I am here of course ignoring the fact that there are more than a hundred apps similar available on the Market.

When developing the first version of this app I ran into an interesting point about UI responsiveness and how I could do a "sleep()" function.

The flow is:

  1. The problem is presented
  2. The user answers by clicking on of the available buttons
  3. Mark the problem green for a second before continuing if the answer was correct
  4. If the asnwer was incorrect mark the problem red until the correct answer is chosen

I ended up doing it as follows

private void verifyResult(final View view){
 answerAttempts++; // The user has tried to answer the problem increment answer attempts
 disableAnswerButtons(); // Disable further user input when the text is green/red otherwise the user can click many times on the buttons
 int result = Integer.parseInt(view.getTag().toString());
 if (correctResult == result)
  problem.setText(getProblemText(true)); // Show full problem + correct result
  // Wait 1 second before resetting the text color and showing a new problem text
  new Handler().postDelayed(new Runnable() { 
   public void run() { 
    generateProblem(); // Generate a new problem and update UI
  }, 1000);
 score.setText(String.format("%s %d/%d", getString(R.string.score), points, answerAttempts));

No comments: