How to create Timer in Android Studio in very simple and easy Steps?

 A Timer in an Android Application is used to set a countdown based on the interval set by the users. You can create this Timer in your Android Application to create any countdown for any particular event such as a countdown for any quiz or test in your application. In this blog, I am going to share a simple way of creating such a Timer.

Desired Result:





SetUp:

Add these codes to your project.

1. In build.gradle (project)

     allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}

2. In build.gradle (module)

     dependencies {
implementation 'com.github.jaeryo2357:circleTimer:1.1.3'
}

After the setup is complete sync your project and create a layout for your Application.

XML file :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<com.mut_jaeryo.circletimer.CircleTimer
android:layout_width="wrap_content"
android:layout_height="200dp"
app:init_position="3360"
app:isOutline="true"
android:id="@+id/circle_timer"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22"
app:show_text="true"></com.mut_jaeryo.circletimer.CircleTimer>

<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginBottom="72dp"
android:padding="10dp"
android:text="Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="72dp"
android:padding="10dp"
android:id="@+id/end_button"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toStartOf="@id/start_button" />

</androidx.constraintlayout.widget.ConstraintLayout>

After successfully creating the layout now comes the Java part. Add the following code in your MainActivity.

MainActivity:


package com.example.circletimer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mut_jaeryo.circletimer.CircleTimer;

public class MainActivity extends AppCompatActivity {
private CircleTimer timer;
private Button start_button,stop_button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer =findViewById(R.id.circle_timer);
start_button=findViewById(R.id.start_button);
stop_button=findViewById(R.id.end_button);

// set the countdown limit (in seconds)
timer.setMaximumTime(10); //here countdown limit is 10 seconds
timer.setInitPosition(10);

// when start button is clicked
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timer.start();
}
});

//when stop button is clicked
stop_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timer.stop();
}
});

// when the desired countdown is over
timer.setBaseTimerEndedListener(new CircleTimer.baseTimerEndedListener() {
@Override
public void OnEnded() {
Toast.makeText(MainActivity.this, "Time is up",
Toast.LENGTH_SHORT)
.show();
timer.reset();
}

});
}
}

If you find my blog useful please do share it with your friends. You can also connect with me on Instagram and YouTube.





Comments