Android Open Source - big_nerd_ranch_book_progress Quiz Activity






From Project

Back to project page big_nerd_ranch_book_progress.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project big_nerd_ranch_book_progress listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bignerdranch.android.geoquiz;
//from   w w  w  .j ava  2  s. c om
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

  private static final String TAG = "QuizActivity";
  private static final String KEY_INDEX = "index";
  
  private Button mTrueButton;
  private Button mFalseButton;
  private ImageButton mNextButton;
  private ImageButton mPrevButton;
  private Button mCheatButton;
  private TextView mQuestionTextView;
  private TrueFalse[] mQuestionBank = new TrueFalse[] {
    new  TrueFalse(R.string.question_oceans, true),
    new  TrueFalse(R.string.question_mideast, true),
    new  TrueFalse(R.string.question_africa, true),
    new  TrueFalse(R.string.question_americas, true),
    new  TrueFalse(R.string.question_asia, true)
  };
  private int mCurrentIndex = 0;
  private boolean mIsCheater;
  
  private void updateQuestion() {
//    Log.d(TAG, "Updating question text for question #" + mCurrentIndex, new Exception());
    
    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);
  }
  
  private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
    
    int messageResId = 0;
    
    if (userPressedTrue == answerIsTrue) {
      messageResId = R.string.correct_toast;
    } else {
      messageResId = R.string.incorrect_toast;
    }
    
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
  }
  
  @TargetApi(11)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      ActionBar actionBar = getActionBar();
      actionBar.setSubtitle("Bodies of Water");
    }
    
    mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
    mQuestionTextView.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();
      }
    });
    
    mTrueButton = (Button)findViewById(R.id.true_button);
//    mTrueButton = (Button)findViewById(R.id.question_text_view);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        checkAnswer(true);
      }
    });
      
    mFalseButton = (Button)findViewById(R.id.false_button);    
    mFalseButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        checkAnswer(false);
      }
    });
    
    mNextButton = (ImageButton)findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();
      }
    });
    
    mPrevButton = (ImageButton)findViewById(R.id.previous_button);
    mPrevButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        mCurrentIndex = mCurrentIndex - 1;
        if (mCurrentIndex < 0) mCurrentIndex = mQuestionBank.length - 1;
        updateQuestion();
      }
    });
    
    mCheatButton = (Button)findViewById(R.id.cheat_button);
    mCheatButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        Intent i = new Intent(QuizActivity.this, CheatActivity.class);
        
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
        i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
        
        startActivityForResult(i, 0);
      }
    });
    
    if (savedInstanceState != null) {
      mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
    }
    
    updateQuestion();
  }
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
  }
  
  @Override
  public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart() called");
  }

  @Override
  public void onPause() {
    super.onPause();
    Log.d(TAG, "onPause() called");
  }

  @Override
  public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume() called");
  }

  @Override
  public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop() called");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
  }
  
  @Override
  public void onActivityResult(int requestCode, int ResultCode, Intent data) {
    if (data == null) {
      return;
    }
    mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
  }

}




Java Source Code List

com.bignerdranch.android.criminalintent.CrimeActivity.java
com.bignerdranch.android.criminalintent.CrimeFragment.java
com.bignerdranch.android.criminalintent.CrimeLab.java
com.bignerdranch.android.criminalintent.CrimeListActivity.java
com.bignerdranch.android.criminalintent.CrimeListFragment.java
com.bignerdranch.android.criminalintent.Crime.java
com.bignerdranch.android.criminalintent.SingleFragmentActivity.java
com.bignerdranch.android.geoquiz.BuildConfig.java
com.bignerdranch.android.geoquiz.CheatActivity.java
com.bignerdranch.android.geoquiz.QuizActivity.java
com.bignerdranch.android.geoquiz.QuizActivity.java
com.bignerdranch.android.geoquiz.TrueFalse.java
com.bignerdranch.android.geoquiz.TrueFalse.java