title: Android Quiz Timer Issues
body: <blockquote>
  <p><strong>IMPLEMENTATION 1 ISSUE</strong></p>
  <p><em><strong>Doubt 1</strong></em>:    I tried timer using progress bar. Its like say 5 seconds per question. However, the progress bar does display countdown values. My issue is after time gets over(value reaches 0 ) , I do not go to next question by default ..</p>
  <p><strong>IMPLEMENTATION 2 ISSUE</strong></p>
  <p><em><strong>Doubt 2</strong></em>:     Also ,how can I set a countdown timer using progress bar which starts at first question and is decreasing values continuously till specified limit.Like say I want all 20 questions in my quiz to be answered in 120 mins.
  So progress bar should be 120,119 ....1 .And after reaching 0,I should be automatically redirected to results page.</p>
  </blockquote>
  <h2>QuestionActivity.java</h2>
  <pre><code>package works.really.good;
  
  import java.sql.Date;
  import java.util.List;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.text.format.DateFormat;
  import android.util.Log;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ProgressBar;
  import android.widget.RadioButton;
  import android.widget.TextView;
  
  import works.really.good.quiz.GamePlay;
  import works.really.good.quiz.Question;
  import works.really.good.util.Utility;
  
  public class QuestionActivity extends Activity implements OnClickListener{
  
      private Question currentQ;
      private GamePlay currentGame;
      ProgressBar mProgressBar;
      CountDownTimer mCountDownTimer;
      TextView tv;
      int i=0;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.question);
          currentGame = ((MyApplication)getApplication()).getCurrentGame();
          currentQ = currentGame.getNextQuestion();
          Button nextBtn = (Button) findViewById(R.id.nextBtn);
          nextBtn.setOnClickListener(this);
          setQuestions();
  
          tv = (TextView) findViewById(R.id.tv_counter);
          mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
          mProgressBar.setProgress(i);
             mCountDownTimer=new CountDownTimer(5000,1000) {
  
                  @Override
                  public void onTick(long millisUntilFinished) {
                      Log.v(&quot;Log_tag&quot;, &quot;Tick of Progress&quot;+ i+ millisUntilFinished);
                      i++;
                      String time = (String) DateFormat.format(&quot;ss&quot;, new Date(millisUntilFinished));
                      tv.setText(time);
                      mProgressBar.setProgress(i);
  
                  }
  
                  @Override
                  public void onFinish() {
                  //Do what you want 
                      i++;
                      mProgressBar.setProgress(i);
                  }
              };
              mCountDownTimer.start();
          
      }
  
  
      private void setQuestions() {
          //set the question text from current question
          String question = Utility.capitalise(currentQ.getQuestion()) + &quot;?&quot;;
          TextView qText = (TextView) findViewById(R.id.question);
          qText.setText(question);
          
          //set the available options
          List&lt;String&gt; answers = currentQ.getQuestionOptions();
          TextView option1 = (TextView) findViewById(R.id.answer1);
          option1.setText(Utility.capitalise(answers.get(0)));
          
          TextView option2 = (TextView) findViewById(R.id.answer2);
          option2.setText(Utility.capitalise(answers.get(1)));
          
          TextView option3 = (TextView) findViewById(R.id.answer3);
          option3.setText(Utility.capitalise(answers.get(2)));
          
          TextView option4 = (TextView) findViewById(R.id.answer4);
          option4.setText(Utility.capitalise(answers.get(3)));
      }
      
  
      @Override
      public void onClick(View arg0) {
              
          /**
           * validate a checkbox has been selected
           */
          if (!checkAnswer()) return;
  
          
          /**
           * check if end of game
           */
          if (currentGame.isGameOver()){
              Intent i = new Intent(this, EndgameActivity.class);
              startActivity(i);
              finish();
          }
          else{
              Intent i = new Intent(this, QuestionActivity.class);
              startActivity(i);
              finish();
          }
      }
      
      
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event)
      {
          switch (keyCode)
          {
          case KeyEvent.KEYCODE_BACK :
              return true;
          }
          
          return super.onKeyDown(keyCode, event);
      }
  
  
      private boolean checkAnswer() {
          String answer = getSelectedAnswer();
          if (answer==null){
              return false;
          }
          else {
                  if (currentQ.getAnswer().equalsIgnoreCase(answer))
              {
                      currentGame.incrementRightAnswers();
              }
              else{
                      currentGame.incrementWrongAnswers();
              }
              return true;
          }
      }
  
  
      private String getSelectedAnswer() {
          RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
          RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
          RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
          RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
          if (c1.isChecked())
          {
              if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
              {
                  c1.setTextColor(Color.GREEN);
              }
              else
              {
                  c1.setTextColor(Color.RED);
                  if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                  {
                      c2.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                  {
                      c3.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                  {
                      c4.setTextColor(Color.GREEN);
                  }
                  
              }
              return c1.getText().toString();
          }
          if (c2.isChecked())
          {
              if(currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
              {
                  c2.setTextColor(Color.GREEN);
              }   
              else
              {
                  c2.setTextColor(Color.RED);
                  if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                  {
                      c1.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                  {
                      c3.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                  {
                      c4.setTextColor(Color.GREEN);
                  }
              }
              return c2.getText().toString();
          }
          if (c3.isChecked())
          {
              if(currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
              {
                  c3.setTextColor(Color.GREEN);
              }
              else
              {
                  c3.setTextColor(Color.RED);
                  if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                  {
                      c1.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                  {
                      c2.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                  {
                      c4.setTextColor(Color.GREEN);
                  }
              }
              return c3.getText().toString();
          }
          if (c4.isChecked())
          {
              if(currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
              {
                  c4.setTextColor(Color.GREEN);
              }
              else
              {
                  c4.setTextColor(Color.RED);
                  if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                  {
                      c1.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                  {
                      c2.setTextColor(Color.GREEN);
                  }
                  else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                  {
                      c3.setTextColor(Color.GREEN);
                  }
              }
              return c4.getText().toString();
          }
          
          return null;
      }
      
  }
  </code></pre>
  <h2>Question.xml</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;fill_parent&quot; android:gravity=&quot;center_horizontal&quot;
      android:background=&quot;@drawable/background&quot;&gt;
  
      &lt;LinearLayout android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;
          android:paddingTop=&quot;15dip&quot; android:paddingBottom=&quot;15dip&quot;
          android:gravity=&quot;center_horizontal&quot;&gt;
  
          &lt;ImageView android:id=&quot;@+id/logo&quot; android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot; android:src=&quot;@drawable/logo2&quot;
              android:contentDescription=&quot;Quiz_Logo&quot; /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;
          android:paddingTop=&quot;5dip&quot; android:paddingBottom=&quot;5dip&quot;
          android:gravity=&quot;center_horizontal&quot; &gt;
  
          &lt;RadioGroup android:layout_width=&quot;fill_parent&quot;
              android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot;
              android:id=&quot;@+id/group1&quot;&gt;
              
              &lt;TextView android:layout_width=&quot;fill_parent&quot;
                  android:layout_height=&quot;wrap_content&quot; android:textColor=&quot;#ffffff&quot;
                  android:textStyle=&quot;bold&quot; android:id=&quot;@+id/question&quot;/&gt;
              
              &lt;RadioButton android:checked=&quot;false&quot; android:id=&quot;@+id/answer1&quot; /&gt;
              
              &lt;RadioButton android:checked=&quot;false&quot; android:id=&quot;@+id/answer2&quot; /&gt;
              
              &lt;RadioButton android:checked=&quot;false&quot; android:id=&quot;@+id/answer3&quot; /&gt;
              
              &lt;RadioButton android:checked=&quot;false&quot; android:id=&quot;@+id/answer4&quot; /&gt;
              
          &lt;/RadioGroup&gt;
  
      &lt;/LinearLayout&gt;
      
      
  
      &lt;LinearLayout android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;
          android:paddingTop=&quot;5dip&quot; android:paddingBottom=&quot;5dip&quot;
          android:gravity=&quot;center_horizontal&quot;&gt;
  
          &lt;Button android:text=&quot;Next&quot; android:id=&quot;@+id/nextBtn&quot;
              android:layout_width=&quot;80dip&quot; 
              android:layout_height=&quot;wrap_content&quot; android:background=&quot;@drawable/button_selector&quot;
              android:paddingTop=&quot;5dip&quot; android:paddingBottom=&quot;5dip&quot;
              android:textColor=&quot;#ffffff&quot; /&gt;              
  
      &lt;/LinearLayout&gt;     
      
      &lt;LinearLayout android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;290.0dip&quot; android:layout_height=&quot;wrap_content&quot;
          android:paddingTop=&quot;5dip&quot; android:paddingBottom=&quot;5dip&quot; android:layout_marginTop=&quot;50dp&quot;
          android:gravity=&quot;center_horizontal&quot; &gt;                   
          
      &lt;ProgressBar 
      android:id=&quot;@+id/progressbar&quot;  
      android:max=&quot;5&quot; 
      android:progress=&quot;0&quot; 
      android:layout_width=&quot;0dip&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_weight=&quot;1.0&quot; 
      style=&quot;@android:style/Widget.ProgressBar.Horizontal&quot;
      /&gt;      
      
      &lt;TextView 
          android:id=&quot;@+id/tv_counter&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;5&quot;/&gt;
      &lt;/LinearLayout&gt;     
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p><img src="https://i.stack.imgur.com/yHtsM.png" alt="This is basically the page where I want timer to display just below next button" /></p>
  <ul>
  <li><p>In picture above is my question activity wherein I want to display and start
  countdown progress timer.In all there are 20 questions in quiz.</p>
  </li>
  <li><p>I want to start timer as soon as question activity is started , time
  interval to be 2    mins(120 sec) and want it to reduce value by 1
  sec continuously till it
  reaches 0.</p>
  </li>
  <li><p>Also I want progress bar to decrease by 1 percent with every second
  elapsed and value(text) displayed alongside progress bar also should
  decrease too simultaneously.</p>
  </li>
  <li><p>Like for 120 sec,progress bar is 100% . It should be 99% for 119 sec
  and so on.</p>
  </li>
  <li><p>After reaching 0 , user should be redirected to results
  page(EndgameActivity.java).</p>
  <p>I am willing to mail my code to anybody willing to help me in case of any doubts in my code.</p>
  </li>
  </ul>