Today I will show you how to create a countdown timer application using the Android Studio version 3.4.1.
The primary reason why I have not written any Android Studio tutorials is a problem with technical resources.
This version of Android Studio seems more promising for the user, but it also has problems that I have encountered.
One problem I find tedious is the gradle configuration.
Another problem is memory loading during development.
I had to use the cleaning system and the IDE clean and build tool to resume development.
All this happened with a simple example.
Let’s begin this tutorial about Countdown timer application using the Android SDK version 8.0.
You can download this application from Download – Application or from this link.
Start with a now Basic Activity project using Java programming language.
You can use Kotlin if you want.
Configure the build.gradle file in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | buildscript { repositories { jcenter() maven { url 'https://maven.google.com' } } dependencies { classpath 'com.android.tools.build:gradle:3.4.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } } } task clean(type: Delete) { delete rootProject.buildDir } |
Use this for activity_main.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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=".MainActivity"> <TextView android:id="@+id/text_view_countdown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="150sp" tools:text="10:00" /> <Button android:id="@+id/button_start_pause" android:layout_width="match_parent" android:layout_height="176dp" android:layout_below="@+id/text_view_countdown" android:layout_centerHorizontal="true" android:text="START" /> <Button android:id="@+id/button_reset" android:layout_width="match_parent" android:layout_height="176dp" android:layout_below="@+id/button_start_pause" android:layout_centerHorizontal="true" android:text="RESET" /> </RelativeLayout> |
The next file is MainActivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | package org.ft.countdowntimer; 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 org.w3c.dom.Text; import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final long TIME_IN_MILLISECONDS = 600000; private TextView myTextViewCountDown; private Button myButtonStartPause; private Button myButtonReset; private CountDownTimer myCountDownTimmer; private boolean is_TimerRunning; private long mTimerLeftInMillis = TIME_IN_MILLISECONDS; private CountDownTimer countDownTimer; private long timeLeftInMilliseconds = 600000; private boolean timeRunning; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTextViewCountDown = findViewById(R.id.text_view_countdown); myButtonStartPause = findViewById(R.id.button_start_pause); myButtonReset = findViewById(R.id.button_reset); myButtonStartPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(is_TimerRunning){ pauseTimer(); } else { startTimer(); } } }); myButtonReset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { resetTimer(); } }); update_CountDownText(); } private void startTimer(){ myCountDownTimmer = new CountDownTimer(mTimerLeftInMillis, 1000) { @Override public void onTick(long miliUntilFinish) { mTimerLeftInMillis = miliUntilFinish; update_CountDownText(); } @Override public void onFinish() { is_TimerRunning = false; myButtonStartPause.setText("Start"); myButtonStartPause.setVisibility(View.INVISIBLE); myButtonReset.setVisibility(View.VISIBLE); } }.start(); is_TimerRunning = true; myButtonStartPause.setText("pause"); myButtonReset.setVisibility(View.INVISIBLE); } private void pauseTimer(){ myCountDownTimmer.cancel(); is_TimerRunning = false; myButtonStartPause.setText("Start"); myButtonReset.setVisibility(View.VISIBLE); } private void resetTimer(){ mTimerLeftInMillis = TIME_IN_MILLISECONDS; update_CountDownText(); myButtonReset.setVisibility(View.INVISIBLE); myButtonStartPause.setVisibility(View.VISIBLE); } private void update_CountDownText(){ int minutes = (int) (mTimerLeftInMillis / 1000) /60; int seconds = (int) (mTimerLeftInMillis / 1000) %60; String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds); myTextViewCountDown.setText(timeLeftFormatted); } } |