title: ProgressBar with a countdowntimer - Android
body: <p>I have a 20 second countdown timer successfully working on my trivia game.  I want to add a <code>ProgressBar</code> (not a <code>ProgressDialog</code>) to the screen.  I found the developer guide for Android confusing.  I googled lots of examples and tried to combine them into my code.  Right now all that displays when I run a game is an empty bar with no "progress" made during each question of the game.</p>
  
  <p><strong>QuestionView.java</strong></p>
  
  <pre><code>public class QuestionView extends Activity  {
  
      int correctAnswers = 0;
      int wrongAnswers = 0;
      int answer = 0;
      int i = 0;
  
      long score = 0;
  
      long startTime = 20000;
      long interval = 1000;
      long points;
  
      boolean timerHasStarted = false;
  
      String category;
  
      Button answer1, answer2, answer3, answer4;
      TextView question, pointCounter, questionNumber, timeCounter;
  
      ArrayList&lt;Question&gt; queries;
      Timer cdTimer;
  
      ProgressBar bar;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.questionviewmain);
  
          answer1 = (Button)findViewById(R.id.answer1);
          answer2 = (Button)findViewById(R.id.answer2);
          answer3 = (Button)findViewById(R.id.answer3);
          answer4 = (Button)findViewById(R.id.answer4);
  
          question = (TextView)findViewById(R.id.question);
  
          category = getIntent().getStringExtra("category");
          queries = getIntent().getParcelableArrayListExtra("queries");
  
          pointCounter = (TextView)findViewById(R.id.timer);
          questionNumber = (TextView)findViewById(R.id.timeElapsedView);
          timeCounter = (TextView)findViewById(R.id.timeCounter);
  
          cdTimer = new Timer(startTime, interval);
  
          bar = (ProgressBar)findViewById(R.id.progressbar);
          bar.setIndeterminate(false);
          bar.setMax(9);
  
          loadQuestion();
      }
  
      public void loadQuestion() {
  
              bar.setProgress(i);
  
          if(i == 10) {
  
              endQuiz();
  
          } else {
  
              if(!timerHasStarted) {
                  cdTimer.start();
                  timerHasStarted = true;
              } else {
                  cdTimer.start();
                  timerHasStarted = false;
              }
  
              answer = queries.get(i).getCorrectAnswer();
  
              question.setText(queries.get(i).getQuery());
  
              answer1.setText(queries.get(i).getA1());
              answer2.setText(queries.get(i).getA2());
              answer3.setText(queries.get(i).getA3());
              answer4.setText(queries.get(i).getA4());
  
              answer1.setOnClickListener(new OnClickListener() {
                  public void onClick(View arg0) {
                      queries.get(i).setSelectedAnswer(0);
                      if(answer == 0) {
                          correctAnswers++;
                          nextQuestion();
                      } else {
                          wrongAnswers++;
                          nextQuestion();
                      }
                  }
              });
  
              answer2.setOnClickListener(new OnClickListener() {
                  public void onClick(View arg0) {
                      queries.get(i).setSelectedAnswer(1);
                      if(answer == 1) {
                          correctAnswers++;
                          nextQuestion();
                      } else {
                          wrongAnswers++;
                          nextQuestion();
                      }
                  }
              });
  
              answer3.setOnClickListener(new OnClickListener() {
                  public void onClick(View arg0) {
                      queries.get(i).setSelectedAnswer(2);
                      if(answer == 2) {
                          correctAnswers++;
                          nextQuestion();
                      } else {
                          wrongAnswers++;
                          nextQuestion();
                      }
                  }
              });
  
              answer4.setOnClickListener(new OnClickListener() {
                  public void onClick(View arg0) {
                      queries.get(i).setSelectedAnswer(3);
                      if(answer == 3) {
                          correctAnswers++;
                          nextQuestion();
                      } else {
                          wrongAnswers++;
                          nextQuestion();
                      }
                  }
              });
          } 
      }
  
      public ArrayList&lt;Question&gt; getQueries() {
          return queries;
      }
  
      public void nextQuestion() {
          score = score + points;
          i++;
          loadQuestion();
      }
  
      public class Timer extends CountDownTimer {
  
          public void startCountdownTimer() {  
          }
  
          public Timer(long startTime, long interval) {
              super(startTime, interval);
          }
  
          @Override
          public void onFinish() {
              if(i &gt;= 9) {
                  cdTimer.cancel();
                  endQuiz();
              } else {
                  wrongAnswers++;
                  nextQuestion();
              }
          }
  
          @Override
          public void onTick(long millisUntilFinished) {
              timeCounter.setText("Time remaining: " + (millisUntilFinished / 100));
              points = (millisUntilFinished / 100) / 2;
              pointCounter.setText("Points remaining: " + points);
              if(i &lt; 10) {
                  questionNumber.setText("Question " + (i + 1) + " of 10");
              }
          }
      }
  
      public void endQuiz() {
          Intent intent = new Intent(QuestionView.this, Results.class);
          intent.putExtra("correctAnswers", correctAnswers);
          intent.putExtra("wrongAnswers", wrongAnswers);
          intent.putExtra("score", score);
          intent.putParcelableArrayListExtra("queries", queries);
          intent.putExtra("category", category);
          startActivity(intent);
      }
  }
  </code></pre>
  
  <p>XML code</p>
  
  <pre><code>&lt;ProgressBar 
                  android:id="@+id/progressbar"
                  android:layout_height="wrap_content" 
                  android:layout_width="wrap_content"
                  android:visibility="visible" 
                  style="@android:style/Widget.ProgressBar.Horizontal" /&gt;
  </code></pre>