Android: Simple Stopwatch Dedicated For Beginners
Submitted by razormist on Saturday, June 3, 2017 - 14:32.
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
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.
App Resources
This contains the all the resources that I have used in the app.
Colors
String
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,
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!!
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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="com.example.arvin.simplestopwatch.MainActivity"
- tools:layout_editor_absoluteY="81dp"
- tools:layout_editor_absoluteX="0dp">
- <TextView
- android:id="@+id/time_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="92sp"
- android:textColor="@color/gray"
- tools:layout_constraintTop_creator="1"
- tools:layout_constraintRight_creator="1"
- app:layout_constraintRight_toRightOf="parent"
- android:layout_marginTop="52dp"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- android:id="@+id/btn_start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="15dp"
- android:layout_marginEnd="10dp"
- android:layout_marginRight="10dp"
- android:background="@color/colorPrimary"
- android:onClick="onClickStart"
- android:text="@string/start"
- android:textColor="@color/white"
- app:layout_constraintBottom_toTopOf="@+id/btn_reset"
- app:layout_constraintRight_toLeftOf="@+id/btn_reset"
- tools:layout_constraintBottom_creator="1"
- tools:layout_constraintRight_creator="1" />
- android:id="@+id/btn_stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="13dp"
- android:layout_marginRight="13dp"
- android:layout_marginTop="15dp"
- android:background="@color/red"
- android:onClick="onClickStop"
- android:text="@string/stop"
- android:textColor="@color/white"
- app:layout_constraintRight_toLeftOf="@+id/btn_start"
- app:layout_constraintTop_toBottomOf="@+id/btn_start"
- tools:layout_constraintRight_creator="1"
- tools:layout_constraintTop_creator="1" />
- android:id="@+id/btn_reset"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="55dp"
- android:layout_marginEnd="50dp"
- android:layout_marginRight="48dp"
- android:background="@color/orange"
- android:onClick="onClickReset"
- android:text="@string/reset"
- android:textColor="@color/white"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- tools:layout_constraintBottom_creator="1"
- tools:layout_constraintRight_creator="1" />
- </android.support.constraint.ConstraintLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.arvin.simplestopwatch">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity"
- android:configChanges="orientation"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="colorPrimary">#3F51B5</color>
- <color name="colorPrimaryDark">#303F9F</color>
- <color name="colorAccent">#FF4081</color>
- <color name="white">#FFF</color>
- <color name="red">#FF0000</color>
- <color name="orange">#FFA500</color>
- <color name="gray">#CCC</color>
- </resources>
- <resources>
- <string name="app_name">Simple Stopwatch</string>
- <string name="start">Start</string>
- <string name="stop">Stop</string>
- <string name="reset">Reset</string>
- </resources>
- package com.example.arvin.simplestopwatch;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private int seconds=0;
- private boolean startRun;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if(savedInstanceState != null){
- seconds = savedInstanceState.getInt("seconds");
- startRun=savedInstanceState.getBoolean("startRun");
- }
- }
- public void onSaveInstanceState(Bundle saveInstanceState){
- saveInstanceState.putInt("seconds", seconds);
- saveInstanceState.putBoolean("startRun", startRun);
- }
- startRun=true;
- }
- startRun=false;
- }
- startRun=false;
- seconds=0;
- }
- final TextView timeView = (TextView)findViewById(R.id.time_view);
- final Handler handler = new Handler();
- @Override
- public void run() {
- int hours = seconds/3600;
- int minutes = (seconds%3600)/60;
- int secs = seconds%60;
- timeView.setText(time);
- if(startRun){
- seconds++;
- }
- handler.postDelayed(this, 100);
- }
- });
- }
- }
Add new comment
- 2215 views