Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countdown Timer required on Android

Tags:

android

Here is the link where i have discussed about a countdown timer of format(mm:ss) in Java:

Java console code for StopWatch/Timer?

Now i want to display (and update) it in a textview in android. Any clue?

like image 925
Muhammad Maqsoodur Rehman Avatar asked Nov 22 '25 09:11

Muhammad Maqsoodur Rehman


1 Answers

To count down

You'll use a TextField and update its content using CountDownTimer, or check Rahul's answer in this same question.

To count up

In your Activity

import android.widget.Chronometer;

...

private Chronometer crono;

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.screen_crono);    

 this.crono = (Chronometer) findViewById(R.id.calling_crono);

    startCrono();
}

public void startCrono() {
 crono.setBase(SystemClock.elapsedRealtime());
 crono.start();
}

To stop it

crono.stop();

In the XML Layout screen_crono

<Chronometer
    android:id="@+id/calling_crono"
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
    android:textStyle="bold"
 android:textSize="14sp"/>

To set it inside a TextView, I don't think that's possible. Place it to its right or to its left, depending on what you want.

If this is not what you wanted, I hope it helps someone else.

like image 51
Maragues Avatar answered Nov 24 '25 21:11

Maragues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!