A Chronometer problem in an android application - android

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?

Related

Disable button after a click for 2 seconds and resume back

When I click a resend button first time, the button will disable for 2 seconds.After 2 seconds the button will enable?
I am using this code
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},1000);
}
});
But this code is not working properly.
try this for this purpose you can use Handler(import android.os.Handler;)
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},2000);// set time as per your requirement
}
});
You can use a timer for this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myButton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
myButton.setEnabled(true);
}
});
}
}, 5000);
}
});
Find the solution
In your button click
long mLastClickTime;
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disableButtonTwoSecs();
// Here your implementation
}
});
public static boolean disableButtonTwoSecs() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 2000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}

Android Quiz App countdown

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

Kill audio after deliberate or accidental exit of dialog

This is part of the code in my MainActivity.
stopwatch1 is an image button called in MainActivity:
stopwatch1 = (ImageButton)findViewById(R.id.stopwatch1);
I have tried searching for clues on how to use dialog.setOnDismissListener, however, there aren't proper answers to help me with the killing of the audio after deliberate or accidental exit of the dialog. Any help would be appreciated.
stopwatch1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog mmDialog1 = new Dialog(context);
mmDialog1.setContentView(R.layout.countdowntimer);
mmDialog1.setTitle("Stop-watch");
Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit);
final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start);
final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview);
ddButton2.setTag(1);
ddButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final int status = (Integer) v.getTag();
if (status == 1) {
ddButton2.setText("Stop");
countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
int time = (int) (millisUntilFinished/1000);
timeview.setText(Integer.toString(time));
}
#Override
public void onFinish() {
timeview.setText("60");
ddButton2.setText("Start");
MediaPlayer stop = MediaPlayer.create(context, R.raw.stop);
stop.start();
}
}.start();
v.setTag(0);
} else {
ddButton2.setText("Start");
timeview.setText("60");
countDownTimer.cancel();
v.setTag(1);
}
}
});
ddButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mmDialog1.dismiss();
}
});
mmDialog1.show();
}
});
My suggestion would be to implement an interface. When the dialog closes, have the callback shut off the media player.
A quick example is this.
interface
public interface DialogClosed {
void shutDownMediaPlayer();
}
then implement the interface in the calling activity
activity
public class MainActivity extends Activity implements DialogClosed {
....
#Override
public void shutDownMediaPlayer() {
//shut down media player here
}
}
Also, you may add a boolean to check if the dialog is showing like this
boolean isShowing = mDialog.isShowing();
and then you can just shut off the media player at any point if this is false.
So I've used some pointers from Jawascript and also edited my code a little, now it works:
stopwatch1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog mmDialog1 = new Dialog(context);
mmDialog1.setContentView(R.layout.countdowntimer);
mmDialog1.setTitle("Stop-watch");
Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit);
final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start);
final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview);
final MediaPlayer stop = MediaPlayer.create(context, R.raw.stop);
ddButton2.setTag(1);
mmDialog1.setCancelable(false);
if (mmDialog1.isShowing() == false) {
stop.stop();
}
ddButton2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
final int status = (Integer) v.getTag();
if (status == 1) {
ddButton2.setText("Stop");
countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
int time = (int) (millisUntilFinished/1000);
timeview.setText(Integer.toString(time));
}
#Override
public void onFinish() {
timeview.setText("60");
ddButton2.setText("Start");
try {
stop.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
stop.start();
}
}.start();
v.setTag(0);
} else {
ddButton2.setText("Start");
timeview.setText("60");
countDownTimer.cancel();
v.setTag(1);
stop.stop();
}
}
});
ddButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mmDialog1.dismiss();
}
});
mmDialog1.show();
}
});

android - Thread.sleep causes lags in app

I want to do app to show 3 elements (each 2 seconds) and each start showing after the before element. Here is code:
public class Remember extends Activity
{
TextView text;
ImageView element1, element2, element3, element4 ,element5, element6;
Button button1, button2, button3, button4, button5, button6;
int data, id;
Random random;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remember);
random = new Random();
id=1;
text = (TextView)findViewById(R.id.text);
element1 = (ImageView)findViewById(R.id.element1);
element2 = (ImageView)findViewById(R.id.element2);
element3 = (ImageView)findViewById(R.id.element3);
element4 = (ImageView)findViewById(R.id.element4);
element5 = (ImageView)findViewById(R.id.element5);
element6 = (ImageView)findViewById(R.id.element6);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
element1.setVisibility(View.GONE);
element2.setVisibility(View.GONE);
element3.setVisibility(View.GONE);
element4.setVisibility(View.GONE);
element5.setVisibility(View.GONE);
element6.setVisibility(View.GONE);
data = random.nextInt(6)+1;
}
public void button1(View v)
{
gameStart();
}
public void gameStart()
{
do{
if (data==1)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element1.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element1.setVisibility(View.GONE);
}
}.start();
}
else if (data==2)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element2.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element2.setVisibility(View.GONE);
}
}.start();
}
else if (data==3)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element3.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element3.setVisibility(View.GONE);
}
}.start();
}
else if (data==4)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element4.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element4.setVisibility(View.GONE);
}
}.start();
}
else if (data==5)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element5.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element5.setVisibility(View.GONE);
}
}.start();
}
else if (data==6)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element6.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element6.setVisibility(View.GONE);
}
}.start();
}
id=id+1;
text.setText("cos " + id);
data = random.nextInt(6)+1;
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}while(id<=3);
}
}
But if I clicked start application start lagging and everything go to norm after time which all elements should be hide. If I delete Thread.sleep all elements show in the same time. What should I do?
Thread.sleep(2000); on the ui thread blocks the ui thread. You should never block the ui thread. Remove the sleep.
To create a delay use a Handler.
Read
http://developer.android.com/guide/components/processes-and-threads.html

how to make the countdown timer to count till zero?

here is the code im using...my countdown timer doesnt display 0(zero) after 1...how to do it...I have tried replacing 1000 with 0 in countdown ...but it brought error while debugging...plss hellppppppp
public class play extends Activity {
TextView mTextField;
TextView score;
ToggleButton pause;
CountDownTimer countdown;
private long a;
private long b;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.game);
mTextField=(TextView)findViewById(R.id.tme);
score=(TextView)findViewById(R.id.score);
score.setText("0" );
final ImageButton menu=(ImageButton)findViewById(R.id.menu);
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(play.this , GoodwordActivity.class);
startActivity(intent);
}
});
Bundle bundle = getIntent().getExtras();
b= bundle.getInt("a");
a=b;
startCountDownTimer();
pause=(ToggleButton)findViewById(R.id.pause);
pause.setText("");
pause.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pause.isChecked()) {
pause.setText("");
pause.setBackgroundResource(R.drawable.refresh);
countdown.cancel();
} else {
pause.setText("");
pause.setBackgroundResource(R.drawable.pause);
startCountDownTimer();
}
}
});
}
private void startCountDownTimer( )
{
countdown=new CountDownTimer(a, 1000) {
public void onTick(long millisUntilFinished ) {
a=millisUntilFinished;
mTextField.setText("" + millisUntilFinished/1000);
}
public void onFinish() {
a=b;
startCountDownTimer();
}
}.start();
}
}
I think you have to put the
mTextField.setText("0");
into the onFinish() function.

Categories

Resources