Android: Simple Stopwatch Dedicated For Beginners

In this tutorial we will try to create a Simple Stopwatch Using Android. Android is most commonly comes installed on a variety of smartphones and tablets, and even in TV. Android is an open source so that developer find it easy to develop and expand new features. So let's do the coding... Getting Started: First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html Layout Design We will now add some layout to the application. Just kindly copy the code below and paste it inside the code editor
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.arvin.simplestopwatch.MainActivity"
  8.     tools:layout_editor_absoluteY="81dp"
  9.     tools:layout_editor_absoluteX="0dp">
  10.  
  11.  
  12.     <TextView
  13.         android:id="@+id/time_view"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:textSize="92sp"
  17.         android:textColor="@color/gray"
  18.         tools:layout_constraintTop_creator="1"
  19.         tools:layout_constraintRight_creator="1"
  20.         app:layout_constraintRight_toRightOf="parent"
  21.         android:layout_marginTop="52dp"
  22.         tools:layout_constraintLeft_creator="1"
  23.         app:layout_constraintLeft_toLeftOf="parent"
  24.         app:layout_constraintTop_toTopOf="parent" />
  25.  
  26.     <Button
  27.         android:id="@+id/btn_start"
  28.         android:layout_width="wrap_content"
  29.         android:layout_height="wrap_content"
  30.         android:layout_marginBottom="15dp"
  31.         android:layout_marginEnd="10dp"
  32.         android:layout_marginRight="10dp"
  33.         android:background="@color/colorPrimary"
  34.         android:onClick="onClickStart"
  35.         android:text="@string/start"
  36.         android:textColor="@color/white"
  37.         app:layout_constraintBottom_toTopOf="@+id/btn_reset"
  38.         app:layout_constraintRight_toLeftOf="@+id/btn_reset"
  39.         tools:layout_constraintBottom_creator="1"
  40.         tools:layout_constraintRight_creator="1" />
  41.  
  42.     <Button
  43.         android:id="@+id/btn_stop"
  44.         android:layout_width="wrap_content"
  45.         android:layout_height="wrap_content"
  46.         android:layout_marginEnd="13dp"
  47.         android:layout_marginRight="13dp"
  48.         android:layout_marginTop="15dp"
  49.         android:background="@color/red"
  50.         android:onClick="onClickStop"
  51.         android:text="@string/stop"
  52.         android:textColor="@color/white"
  53.         app:layout_constraintRight_toLeftOf="@+id/btn_start"
  54.         app:layout_constraintTop_toBottomOf="@+id/btn_start"
  55.         tools:layout_constraintRight_creator="1"
  56.         tools:layout_constraintTop_creator="1" />
  57.  
  58.     <Button
  59.         android:id="@+id/btn_reset"
  60.         android:layout_width="wrap_content"
  61.         android:layout_height="wrap_content"
  62.         android:layout_marginBottom="55dp"
  63.         android:layout_marginEnd="50dp"
  64.         android:layout_marginRight="48dp"
  65.         android:background="@color/orange"
  66.         android:onClick="onClickReset"
  67.         android:text="@string/reset"
  68.         android:textColor="@color/white"
  69.         app:layout_constraintBottom_toBottomOf="parent"
  70.         app:layout_constraintRight_toRightOf="parent"
  71.         tools:layout_constraintBottom_creator="1"
  72.         tools:layout_constraintRight_creator="1" />
  73.  
  74.  
  75.  
  76. </android.support.constraint.ConstraintLayout>
Android Manifest File The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.arvin.simplestopwatch">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.  
  16.             <intent-filter>
  17.                 <action android:name="android.intent.action.MAIN" />
  18.  
  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.             </intent-filter>
  21.         </activity>
  22.     </application>
  23.  
  24. </manifest>
App Resources This contains the all the resources that I have used in the app. Colors
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <color name="colorPrimary">#3F51B5</color>
  4.     <color name="colorPrimaryDark">#303F9F</color>
  5.     <color name="colorAccent">#FF4081</color>
  6.     <color name="white">#FFF</color>
  7.     <color name="red">#FF0000</color>
  8.     <color name="orange">#FFA500</color>
  9.     <color name="gray">#CCC</color>
  10. </resources>
String
  1. <resources>
  2.     <string name="app_name">Simple Stopwatch</string>
  3.     <string name="start">Start</string>
  4.     <string name="stop">Stop</string>
  5.     <string name="reset">Reset</string>
  6. </resources>
The Main Function This code contains the main function of the application. This code automatically run the time when the user touch the start button, and simultaneously stop when the stop button touch, at the same time reset when the reset button touch,
  1. package com.example.arvin.simplestopwatch;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.widget.TextView;
  8.  
  9. public class MainActivity extends Activity {
  10.  
  11.     private int seconds=0;
  12.     private boolean startRun;
  13.  
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         if(savedInstanceState != null){
  20.           seconds = savedInstanceState.getInt("seconds");
  21.             startRun=savedInstanceState.getBoolean("startRun");
  22.         }
  23.  
  24.         Timer();
  25.  
  26.  
  27.     }
  28.  
  29.     public void onSaveInstanceState(Bundle saveInstanceState){
  30.         saveInstanceState.putInt("seconds", seconds);
  31.         saveInstanceState.putBoolean("startRun", startRun);
  32.     }
  33.  
  34.     public void onClickStart(View view){
  35.         startRun=true;
  36.     }
  37.  
  38.     public void onClickStop(View view){
  39.         startRun=false;
  40.     }
  41.  
  42.     public void onClickReset(View view){
  43.         startRun=false;
  44.         seconds=0;
  45.     }
  46.  
  47.     private void Timer(){
  48.         final TextView timeView = (TextView)findViewById(R.id.time_view);
  49.         final Handler handler = new Handler();
  50.         handler.post(new Runnable() {
  51.             @Override
  52.             public void run() {
  53.                 int hours = seconds/3600;
  54.                 int minutes = (seconds%3600)/60;
  55.                 int secs = seconds%60;
  56.  
  57.                 String time = String.format("%d:%02d:%02d", hours, minutes, secs);
  58.  
  59.                 timeView.setText(time);
  60.  
  61.                 if(startRun){
  62.                     seconds++;
  63.                 }
  64.  
  65.                 handler.postDelayed(this, 100);
  66.             }
  67.         });
  68.  
  69.     }
  70. }
There you have it we successfully created a Simple Stopwatch Using Android. I hope that this simple tutorial help you to your projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Tags

Add new comment