Created
September 25, 2021 14:40
-
-
Save codinginflow/58fddb4dcdb35ce7a7ff78aaa607c6ee to your computer and use it in GitHub Desktop.
CountDownTimer Tutorial Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.codinginflow.countdowntimerexample.MainActivity"> | |
<TextView | |
android:id="@+id/text_view_countdown" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="00:00" | |
android:textColor="@android:color/black" | |
android:textSize="60sp" /> | |
<Button | |
android:id="@+id/button_start_pause" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/text_view_countdown" | |
android:layout_centerHorizontal="true" | |
android:text="start" /> | |
<Button | |
android:id="@+id/button_reset" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/text_view_countdown" | |
android:layout_marginStart="11dp" | |
android:layout_toEndOf="@+id/button_start_pause" | |
android:text="reset" | |
android:visibility="invisible" | |
tools:visibility="visible" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.countdowntimerexample; | |
import android.os.CountDownTimer; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import java.util.Locale; | |
public class MainActivity extends AppCompatActivity { | |
private static final long START_TIME_IN_MILLIS = 600000; | |
private TextView mTextViewCountDown; | |
private Button mButtonStartPause; | |
private Button mButtonReset; | |
private CountDownTimer mCountDownTimer; | |
private boolean mTimerRunning; | |
private long mTimeLeftInMillis = START_TIME_IN_MILLIS; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mTextViewCountDown = findViewById(R.id.text_view_countdown); | |
mButtonStartPause = findViewById(R.id.button_start_pause); | |
mButtonReset = findViewById(R.id.button_reset); | |
mButtonStartPause.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mTimerRunning) { | |
pauseTimer(); | |
} else { | |
startTimer(); | |
} | |
} | |
}); | |
mButtonReset.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
resetTimer(); | |
} | |
}); | |
updateCountDownText(); | |
} | |
private void startTimer() { | |
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) { | |
@Override | |
public void onTick(long millisUntilFinished) { | |
mTimeLeftInMillis = millisUntilFinished; | |
updateCountDownText(); | |
} | |
@Override | |
public void onFinish() { | |
mTimerRunning = false; | |
mButtonStartPause.setText("Start"); | |
mButtonStartPause.setVisibility(View.INVISIBLE); | |
mButtonReset.setVisibility(View.VISIBLE); | |
} | |
}.start(); | |
mTimerRunning = true; | |
mButtonStartPause.setText("pause"); | |
mButtonReset.setVisibility(View.INVISIBLE); | |
} | |
private void pauseTimer() { | |
mCountDownTimer.cancel(); | |
mTimerRunning = false; | |
mButtonStartPause.setText("Start"); | |
mButtonReset.setVisibility(View.VISIBLE); | |
} | |
private void resetTimer() { | |
mTimeLeftInMillis = START_TIME_IN_MILLIS; | |
updateCountDownText(); | |
mButtonReset.setVisibility(View.INVISIBLE); | |
mButtonStartPause.setVisibility(View.VISIBLE); | |
} | |
private void updateCountDownText() { | |
int minutes = (int) (mTimeLeftInMillis / 1000) / 60; | |
int seconds = (int) (mTimeLeftInMillis / 1000) % 60; | |
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds); | |
mTextViewCountDown.setText(timeLeftFormatted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want a timer for 2min what can I do?