• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Very simple countdown timmer Help!!!

Mikeyfire

Lurker
Nov 8, 2010
3
0
I`m trying to develop my first android app..it`s a simple trivia game .On one screen I have a TextViev that shows the question and buttons to choose an answer ....I want to add a countdown timer in another TextView .. here is the code I have ...

I need to know where to add the code for the Countdown Timer.The is driving me crazy ....any help would be appreciated

package com.Tutorial;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;


public class Question2 extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question2);

// set a click listener on the toast button
Button Button01 = (Button) findViewById(R.id.Button01);
Button01.setOnClickListener(this);

// set a click listener on the alert button
Button Button02 = (Button) findViewById(R.id.Button02);
Button02.setOnClickListener(this);

// set a click listener on the yesno button
Button Button03 = (Button) findViewById(R.id.Button03);
Button03.setOnClickListener(this);

// set a click listener on the progress button
Button Button04 = (Button) findViewById(R.id.Button04);
Button04.setOnClickListener(this);

Button Button05 = (Button)findViewById(R.id.Button05);
Button05.setOnClickListener(this);


}

public void onClick(View view) {
// which button is clicked?

// the Toast button
if (view == findViewById(R.id.Button01)) {

// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

// set the message to display
alertbox.setMessage("incorrect!");

// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
Intent MenuIntent = new Intent(Question2.this,Menu.class);
startActivity(MenuIntent);
finish ();
}
});

// show it
alertbox.show();
}

if (view == findViewById(R.id.Button02)) {

// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

// set the message to display
alertbox.setMessage("incorrect!");

// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
Intent MenuIntent = new Intent(Question2.this,Menu.class);
startActivity(MenuIntent);
finish ();
}
});

// show it
alertbox.show();

}




if (view == findViewById(R.id.Button03)) {

// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

// set the message to display
alertbox.setMessage("CORRECT!");

// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
Intent Question3Intent = new Intent(Question2.this,Question3.class);
startActivity(Question3Intent);
finish ();
}
});

// show it
alertbox.show();



}
if (view == findViewById(R.id.Button04)) {

// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

// set the message to display
alertbox.setMessage("incorrect ");

// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
Intent MenuIntent = new Intent(Question2.this,Menu.class);
startActivity(MenuIntent);
finish ();
}
});

// show it
alertbox.show();
}

if (view == findViewById(R.id.Button05)) {
// display the toast popup window
Intent MenuIntent = new Intent(Question2.this,Menu.class);
startActivity(MenuIntent);

}
};
}
 
I doubt an article from 2007 is going to help ....anyway maybe I can be more specific ...I need to add the following code to the code listed above .When the code below is finished and the counter reads "DONE" i`d like to also add an action that will bring up a dialog box with a button ..that button will bring me back to the main menu

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class Countdown extends Activity {
TextView timeDisplay;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
timeDisplay = new TextView(this);
this.setContentView(timeDisplay);
MyCount counter = new MyCount(30000, 1000);
counter.start();
}

public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

public void onFinish() {
timeDisplay.setText("Done!");
}

public void onTick(long millisUntilFinished) {
timeDisplay.setText("Left: " + millisUntilFinished / 1000);
}
}
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones