Skip to content

Commit

Permalink
Fixes #47
Browse files Browse the repository at this point in the history
Created the timer and it works perfectly
  • Loading branch information
Volpym committed Oct 13, 2019
1 parent 70d0d0d commit 479ede5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
78 changes: 68 additions & 10 deletions app/src/main/java/com/example/multiplechoice/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;


import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Timer;

import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Expand All @@ -29,9 +26,16 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private Bundle savedInstanceState;
PointSystem pointSystem = new PointSystem();
int iterator = 0;
private CountDownTimer countDownTimer;
private long timeLeft;




private TextView tQuestion;
private TextView questionNumber;
private TextView score;
private TextView timer;
private Button answer1;
private Button answer2;
private Button answer3;
Expand All @@ -40,6 +44,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

private Toast correct_message;
private Toast wrong_message;
private Toast out_of_time;
final Handler handler = new Handler();


Expand All @@ -54,12 +59,15 @@ protected void onCreate(Bundle savedInstanceState) {
//Sets the messages that are going to be displayed when the user selects and answer.
correct_message = Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT);
wrong_message = Toast.makeText(getApplicationContext(),"Wrong!", Toast.LENGTH_SHORT);
out_of_time = Toast.makeText(getApplicationContext(),"Out of Time!", Toast.LENGTH_SHORT);


setContentView(R.layout.activity_main);

questionNumber=findViewById(R.id.eNumberOfQuestions);
tQuestion = findViewById(R.id.textViewQuestion);
score = findViewById(R.id.eScore);
timer = findViewById(R.id.timer);
answer1 = findViewById(R.id.bAnswer1);
answer2 = findViewById(R.id.bAnswer2);
answer3 = findViewById(R.id.bAnswer3);
Expand Down Expand Up @@ -121,22 +129,21 @@ public void loadQuestionElements(Question question) throws InterruptedException


this.correctAnswer = question.getCorrectAnswer();

startCountdown();
}

@Override
public void onClick(View v) {
Button button = (Button) v;



switch(v.getId()) {
case R.id.bAnswer1:
if (this.question.isCorrect(button.getText().toString(), this.correctAnswer)) {
pointSystem.increasePoints();
this.correct_message.show();
countDownTimer.cancel();
} else {
this.wrong_message.show();
countDownTimer.cancel();
}

handler.postDelayed(new Runnable(){
Expand All @@ -149,16 +156,18 @@ public void run(){
e.printStackTrace();
}
}
},1500);
},1000);
break;


case R.id.bAnswer2:
if (this.question.isCorrect(button.getText().toString(), this.correctAnswer)) {
pointSystem.increasePoints();
this.correct_message.show();
countDownTimer.cancel();
} else {
this.wrong_message.show();
countDownTimer.cancel();
}
handler.postDelayed(new Runnable(){
@Override
Expand All @@ -175,8 +184,10 @@ public void run(){
if (this.question.isCorrect(button.getText().toString(), this.correctAnswer)) {
pointSystem.increasePoints();
this.correct_message.show();
countDownTimer.cancel();
} else {
this.wrong_message.show();
countDownTimer.cancel();
}
handler.postDelayed(new Runnable(){
@Override
Expand All @@ -194,8 +205,10 @@ public void run(){
if (this.question.isCorrect(button.getText().toString(), this.correctAnswer)) {
pointSystem.increasePoints();
this.correct_message.show();
countDownTimer.cancel();
} else {
this.wrong_message.show();
countDownTimer.cancel();
}
handler.postDelayed(new Runnable(){
@Override
Expand Down Expand Up @@ -234,6 +247,51 @@ public String showQuestionNumber(int iterator){
return qNumber;
}

private void startCountdown(){
countDownTimer = new CountDownTimer(31000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeft = millisUntilFinished;
updateCountdownText();
}

@Override
public void onFinish() {
out_of_time.show();
countDownTimer.cancel();
updateCountdownText();
handler.postDelayed(new Runnable(){
@Override
public void run(){
try {
loadQuestionElements(getNewQuestion());

} catch (InterruptedException e) {
e.printStackTrace();
}
}
},1000);

}
}.start();
}

private void updateCountdownText(){

int seconds = (int) (timeLeft / 1000) % 60;

String timeFormatted = String.valueOf(seconds);

timer.setText(timeFormatted);

if (timeLeft < 10000) {
timer.setTextColor(Color.RED);
}else{
timer.setTextColor(Color.BLACK);
}
}


}


Expand Down
16 changes: 15 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
android:layout_width="80dp"
android:layout_height="20dp"
android:text="0/100"
android:textSize="12dp"
android:textAlignment="viewEnd"
android:textSize="12dp"
app:layout_constraintEnd_toEndOf="@+id/textViewQuestion"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/eScore"
app:layout_constraintTop_toBottomOf="@+id/textViewQuestion" />

<TextView
Expand Down Expand Up @@ -91,4 +93,16 @@
app:layout_constraintStart_toStartOf="@+id/bAnswer3"
app:layout_constraintTop_toBottomOf="@+id/bAnswer3" />

<TextView
android:id="@+id/timer"
android:layout_width="90dp"
android:layout_height="80dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="@+id/textViewQuestion"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/textViewQuestion"
app:layout_constraintTop_toBottomOf="@+id/textViewQuestion" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 479ede5

Please sign in to comment.