Best Way to Create Timer App in Android
Hello Guys! in this article, I will tell you about an android app concept of timer app. Many people ask me that how can we create a timer app in android. So if you also have this question and if you are finding the best way to create timer app in android then you are at right place. Here you can get information for the best way to create timer app in android. In this article, you can get the source code for creating a timer app in android.
Source code for Timer App in Android
activity_main.xml
<?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" android:background="@drawable/background" tools:context="sumit.net.timerapp.MainActivity"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/timer" android:textSize="40dp" android:textColor="#000000" android:text="00:00:00" android:paddingLeft="100dp" android:paddingTop="50dp"/> <Button android:layout_width="match_parent" android:layout_height="50sp" android:layout_below="@+id/timer" android:layout_marginTop="20dp" android:text="Start" android:background="@drawable/button" android:paddingLeft="20dp" android:id="@+id/start" /> <Button android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/start" android:layout_marginTop="20dp" android:text="Pause" android:background="@drawable/button" android:paddingLeft="10dp" android:id="@+id/pause" /> </RelativeLayout>
MainActivity.java
package sumit.net.timerapp; import android.animation.TimeAnimator; import android.media.TimedText; import android.os.Handler; import android.os.SystemClock; import android.os.health.TimerStat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.Time; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import org.w3c.dom.Text; import java.util.Timer; public class MainActivity extends AppCompatActivity { TextView t1; Button b1,b2; long startTime=0L; private Handler customHandler=new Handler(); long timeInMilliseconds=0L; long timeSwapBuff=0L; long updateTime=0L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1=(TextView)findViewById(R.id.timer); b1=(Button)findViewById(R.id.start); b2=(Button)findViewById(R.id.pause); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startTime= SystemClock.uptimeMillis(); customHandler.postDelayed(updateTimerThread, 0); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timeSwapBuff +=timeInMilliseconds; customHandler.removeCallbacks(updateTimerThread); } }); } private Runnable updateTimerThread=new Runnable() { @Override public void run() { timeInMilliseconds=SystemClock.uptimeMillis()-startTime; updateTime=timeSwapBuff+timeInMilliseconds; int sec=(int) (updateTime/600); int mins=sec/60; sec=sec%60; int milliseconds=(int) (updateTime%100); t1.setText(""+ mins + ":"+ String.format("%02d",sec)+ ":"+String.format("%03d",milliseconds)); customHandler.postDelayed(this,0); } }; }
Output of Time App
Conclusion
So you got information about the best way to create a timer app in android. I hope you understand the method to create a timer app in android. Thank you for reading this article till the end. If you love this article then please share this article in your social media platforms. And share your thought in the comment section below. If you have any question regarding this article then write your question in the comment section. I will try to give the answer to them.