Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a simple Timer in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 534
    Comment on it

    In this tutorial we will make a simple android timer application using a Handler.In the layout of the main activity we will set a TextView to Display the timer and to Buttons for Start and Pause. We set the string resource for the timer value.

    Set the String Resource as this

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app&#95;name">"TimerSet "</string>
        <string name="action&#95;settings">Settings</string>
        <string name="hello&#95;world">Hello world!</string>
    
        <string name="timmerValue">00:00:00</string>
    
    </resources>
    

    The xml layout if the main activity will look like this :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout&#95;width="match&#95;parent"
        android:layout&#95;height="match&#95;parent"
        android:paddingBottom="@dimen/activity&#95;vertical&#95;margin"
        android:paddingLeft="@dimen/activity&#95;horizontal&#95;margin"
        android:paddingRight="@dimen/activity&#95;horizontal&#95;margin"
        android:paddingTop="@dimen/activity&#95;vertical&#95;margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout&#95;width="wrap&#95;content"
            android:layout&#95;height="wrap&#95;content"
            android:layout&#95;alignParentLeft="true"
            android:layout&#95;alignParentTop="true"
            android:layout&#95;marginLeft="70dp"
            android:layout&#95;marginTop="102dp"
            android:textSize="40sp"
            android:text="@string/timmerValue" />
    
        <Button
            android:id="@+id/button1"
            android:layout&#95;width="80dp"
            android:layout&#95;height="50dp"
            android:layout&#95;alignParentLeft="true"
            android:layout&#95;below="@+id/textView1"
            android:layout&#95;marginLeft="30dp"
            android:layout&#95;marginTop="84dp"
            android:text="Start" />
    
        <Button
            android:id="@+id/button2"
            android:layout&#95;width="80dp"
            android:layout&#95;height="50dp"
            android:layout&#95;marginTop="232dp"
            android:layout&#95;alignLeft="@+id/button1"
            android:layout&#95;marginLeft="150dp"
            android:text="Pause" />
    
    </RelativeLayout>
    

    in the MainActivity write the following code, the code is very simple and self explanatory

    package com.example.timerset;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private Button startButton;
        private Button pauseButton;
    
        private TextView timerValue;
    
        private long startTime = 0L;
    
        private Handler customHandler = new Handler();
    
        long timeInMilliseconds = 0L;
        long timeSwapBuff = 0L;
        long updatedTime = 0L;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity&#95;main);
    
            timerValue = (TextView) findViewById(R.id.textView1);
    
            startButton = (Button) findViewById(R.id.button1);
    
            startButton.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View view) {
                    startTime = SystemClock.uptimeMillis();
                    customHandler.postDelayed(updateTimerThread, 0);
    
                }
            });
    
            pauseButton = (Button) findViewById(R.id.button2);
    
            pauseButton.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View view) {
    
                    timeSwapBuff += timeInMilliseconds;
                    customHandler.removeCallbacks(updateTimerThread);
    
                }
            });
        }
    
        private Runnable updateTimerThread = new Runnable() {
    
                        public void run() {
    
                            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
    
                            updatedTime = timeSwapBuff + timeInMilliseconds;
    
                            int secs = (int) (updatedTime / 1000);
                            int mins = secs / 60;
                            secs = secs % 60;
                            int milliseconds = (int) (updatedTime % 1000);
                            timerValue.setText("" + mins + ":"
                                    + String.format("%02d", secs) + ":"
                                    + String.format("%03d", milliseconds));
                            customHandler.postDelayed(this, 0);
                        }
    
                    };
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

    This basic code can be use d in different kind of android applications like stopwatch,interval timer etc. Hope the code helped you.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: