Android Quiz App countdown - android

my code is given below in that for Each Question there is i.e. when count down ends its jump to next question and count down starts again, i want that when the countdown ends it should jump to the result page..and even after jumping to next question countdown should not stop.
#Override
protected void onResume()
{
super.onResume();
questionPlay = db.getRuleQuestionMode(mode);
totalQuestion = questionPlay.size();
mCountDown = new CountDownTimer(TIMEOUT, INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
progressBar.setProgress(progressValue);
progressValue++;
}
#Override
public void onFinish() {
mCountDown.cancel();
}
};
showQuestion(index);
db.close();
}
private void showQuestion(final int index) {
if (index < totalQuestion) {
thisQuestion++;
txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
txtView1.setText(questionPlay.get(index).getQus());
btnA.setText(questionPlay.get(index).getAnswerA());
btnB.setText(questionPlay.get(index).getAnswerB());
btnC.setText(questionPlay.get(index).getAnswerC());
btnD.setText(questionPlay.get(index).getAnswerD());
mCountDown.start();
Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StyleableToast st= new StyleableToast(getApplicationContext(),questionPlay.get(index).getCorrectAnswer(), Toast.LENGTH_SHORT);
st.setBackgroundColor(Color.RED);
st.setTextColor(Color.WHITE);
st.setCornerRadius(3);
st.show();
score-=10;
}
});
} else {
Intent intent = new Intent(this, Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL", totalQuestion);
dataSend.putInt("CORRECT", correctAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
#Override
public void onClick(View v) {
mCountDown.cancel();
if (index < totalQuestion) {
Button clickedButton = (Button) v;
if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())) {
score += 10; // increase score
correctAnswer++; //increase correct answer
showQuestion(++index);
} else {
vibrator.vibrate(50);
showQuestion(++index); // If choose right , just go to next question
}
txtScore.setText(String.format("%d", score));
//clickedButton.setBackgroundColor(Color.YELLOW);;
}
}
#Override
public void onStop() {
if(mCountDown != null)
mCountDown.cancel();
super.onStop();
}

remove mCountDown.start(); in your showQuestion method.
in your onFinish method, call your results page from there

Related

A Chronometer problem in an android application

First I create a Chronometer instance.
Chronometer chronometer;
Then I create Start, Stop, On Tick events.
#Override
protected void onCreate(Bundle savedInstanceState)
{
chronometer = new Chronometer(this);
btn_start.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
TimerStart();
}
});
btn_stop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
TimerStop();
}
});
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener()
{
#Override
public void onChronometerTick(Chronometer chronometerChanged)
{
chronometer = chronometerChanged;
count++;
txt_count.setText(String.valueOf(count));
if (count >= seconds)
{
count = 0;
//play sound
}
}
});
}
Ans Start, Stop methods
void TimerStart()
{
seconds = Long.parseLong(txt_seconds.getText().toString());
count = 0;
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
is_running = true;
}
void TimerStop()
{
chronometer.stop();
is_running = false;
}
Then I start the chronometer and set a break point on count++; line in onChronometerTick event.
But I get there only once and it stops.
What do I miss?

Sound keeps on playing even when app is killed

I'm making a quiz app in which there is a sound game.But i find that sound keeps playing even when app is closed. Two time I'm using mediaplayer.
1.while clicking on image button.
2.on changing question.
Question is changed every 15 sec and after killing app sound is getting played every 15 seconds.
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
btnA.setOnClickListener(this);
btnB.setOnClickListener(this);
btnC.setOnClickListener(this);
btnD.setOnClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
}
#Override
protected void onResume()
{
super.onResume();
questionPlay = db.getMorseQuestionMode(mode);
totalQuestion = questionPlay.size();
mCountDown = new CountDownTimer(TIMEOUT, INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
progressBar.setProgress(progressValue);
progressValue++;
}
#Override
public void onFinish() {
mCountDown.cancel();
showQuestion(++index);
}
};
showQuestion(index);
}
private void showQuestion(int index) {
if (index < totalQuestion) {
thisQuestion++;
txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
int QusId = this.getResources().getIdentifier(questionPlay.get(index).getQus().toString(), "raw", getPackageName());
btnA.setText(questionPlay.get(index).getAnswerA());
btnB.setText(questionPlay.get(index).getAnswerB());
btnC.setText(questionPlay.get(index).getAnswerC());
btnD.setText(questionPlay.get(index).getAnswerD());
mCountDown.start();
mediaPlayer= MediaPlayer.create(this,QusId);
mediaPlayer.start();
} else {
Intent intent = new Intent(this, Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL", totalQuestion);
dataSend.putInt("CORRECT", correctAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
#Override
public void onClick(View v) {
mCountDown.cancel();
if (index < totalQuestion) {
Button clickedButton = (Button) v;
if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())) {
score += 10; // increase score
correctAnswer++; //increase correct answer
showQuestion(++index);
} else {
vibrator.vibrate(50);
showQuestion(++index); // If choose right , just go to next question
}
txtScore.setText(String.format("%d", score));
}
}
//CLicking back Button
public void onBackPressed() {
showExitAlertBox();
}
public void showExitAlertBox() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage("You want to quit the play?");
//Yes Quit then go to category page
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int button) {
Intent intent = new Intent(getApplicationContext(), CategorySecond.class);
startActivity(intent);
}
});
//no Quit stay on same page
alertDialog.setNegativeButton("NO", null);
alertDialog.show();
mediaPlayer.stop();
}
}
Error Showing us below
W/MediaPlayer-JNI: MediaPlayer finalized without being released
Cancel your timer and stop mediaplayer if playing in onStop
#Override
public void onStop() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
if(mCountDown != null)
mCountDown.cancel();
super.onStop();
}
handle on destroy
#Override
public void onDestroy() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
if(mCountDown != null)
mCountDown.cancel();
}
You have not released the mediaplayer properly.
Use
protected void onStop(){
mediaPlayer.release();
mediaPlayer = null;
}
call this method where ever required.

How to implement two countdown timers alternatively in Android

I have implemented one counter and onFinish() of first counter,I started second counter but the first counter not able to finish.Text "Bye Guyz" remain for some time so how to finish the text.
Please help me.
Thanks in advance.!!!
Code :-
counter= new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
if (count == 0) {
tv.setText("First counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Guyz");
}
}
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
else if(flag) {
counter1 = new CountDownTimer(9000, 1000) {
public void onTick(long millisUntilFinished) {
flag = false;
tv.setText("Second counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Girls");
count = 0;
}
public void onFinish() {
tv2.setVisibility(View.VISIBLE);
tv2.setText("Bye Girls");
count = 0;
}
}.start();
Did you "debug" the code to be sure the code is arriving to counter1 = new CountDownTimer(9000, 1000)?
Are you sure when the first counter arrives to onFinish() the flag variable is true?
Why do you call counter.cancel() in onFinish() when obviously the counter is already over?
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
If you say your tv2 displays "Bye Guyz" it means that your flag is set to false, so the "else if" part is not being executed. onFinish() is only executed once, so you need to make sure the flag is set for true to start the second counter.
Also you shouldn't cancel your counter in onFinish() because it's already finished.
Here is my alternative is as follows
Create the custom Counterextending Thread
class Counter extends Thread {
private long timeOne, timeTwo;
private OnCounterFinishedListener mCounterFinishedListener;
private Thread t;
Activity activity = null;
Counter(Context context){
t = new Thread(this);
activity = (Activity)context;
}
#Override
public void run() {
try {
sleep(timeOne);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.firstCounterFinished();
}
});
sleep(timeTwo);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.secondCounterFinished();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void setTimes(long timeOne, long timeTwo){
this.timeOne = timeOne;
this.timeTwo = timeTwo;
}
public void start(OnCounterFinishedListener listener){
mCounterFinishedListener = listener;
t.start();
}
interface OnCounterFinishedListener{
void firstCounterFinished();
void secondCounterFinished();
}
}
Then inside your main thread you can start this counter as
final Counter counter = new Counter(this);
counter.setTimes(5000, 5000);
counter.start(new Counter.OnCounterFinishedListener() {
#Override
public void firstCounterFinished() {
// Update your first TextView
}
#Override
public void secondCounterFinished() {
// Update your second TextView
}
});

android how to add two countdowntimer in single activity?

I am using two imageview win which I want to use two countdowntimer. 1 timer shows 1 image at 1000ms ,after that another countdowntimer start and shows 2 image and after second image go on another screen but I have used this with single countdowntimer. How can I use 2 countdowntimer in my class? Here is my code:
public class MainActivity extends Activity {
private ImageView image1,image2;
CountDownTimer countdown1,countdown2;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
if(a == 0)
{
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
}
else {
} a++;
}
public void onFinish() {
image2.setVisibility(View.VISIBLE);
image1.setVisibility(View.VISIBLE);
Intent i=new Intent(MainActivity.this,NextActivity.class);
startActivity(i);
}
}.start();
}
}
I am not sure if I understood the request but i would try something like this:
public class MainActivity extends Activity {
private ImageView image1,image2;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
if(a == 0) {
image1.setVisibility(View.VISIBLE);
} else if(a == 1) {
image1.setVisibility(View.INVISIBLE);
image2.setVisibility(View.VISIBLE);
}
a++;
}
public void onFinish() {
Intent i=new Intent(MainActivity.this,NextActivity.class);
startActivity(i);
}
}.start();
}
}

How to finish CountdownTimer on back button

I want to finish my CountdownTimer on Back button. I know that there a a few similar questions but that doesn´t help me with my problem.
I´ve got the following code in onCreate()
#Override
public void onFinish() {
new CountDownTimer(10000, 550) {
#Override
public void onTick(long millisUntilFinished) {
for(int i = 0; i< arr.size(); i++){
Button aga = arr.get(i);
if(aga.getVisibility() == View.VISIBLE){
aga.setVisibility(View.GONE);
}
}
int zufall = (int) (Math.random()*23);
setNextButton(arr.get(zufall));
}
#Override
public void onFinish() {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
new CountDownTimer(10000, 350) {
#Override
public void onTick(long millisUntilFinished) {
for(int i = 0; i< arr.size(); i++){
Button aga = arr.get(i);
if(aga.getVisibility() == View.VISIBLE){
aga.setVisibility(View.GONE);
}
}
int zufall = (int) (Math.random()*23);
setNextButton(arr.get(zufall));
System.out.println("HHH");
}
#Override
public void onFinish() {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
highscore.putExtra("count", count);
startActivity(highscore);
}
}.start();
}
}.start();
}
}.start();
Now what I need is, if the user presses the back button during the CountdownTimer to finish my activity. Any solutions for that problem?
Call CountDownTimer.cancel():
http://developer.android.com/reference/android/os/CountDownTimer.html#cancel()
store reference to current CountDownTimer instance in your activity during creation, to have access to it inside Activity.

Categories

Resources