how to create stopwatch in android studio source code
Hello Guys. Welcome to our new tutorial of how to create a stopwatch in android studio source code. this android studio source code will help you to create basic stopwatch application.
please follow the code given below to create stopwatch in android studio source code
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.widget.Button; import android.widget.Chronometer; public class StopWatchApp extends AppCompatActivity { private Chronometer chronometer; private boolean start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_stop_watch_app); chronometer = findViewById(R.id.time); chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometerChanged) { chronometer = chronometerChanged; } }); } public void startStopChronometer(View view){ if(start){ chronometer.stop(); start = false; ((Button)view).setText("Start"); }else{ chronometer.setBase(SystemClock.elapsedRealtime()); chronometer.start(); start = true; ((Button)view).setText("Stop"); } } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".StopWatchApp"> <Chronometer android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:layout_marginBottom="190dp" android:textAlignment="center" android:textColor="#ad1457" android:textSize="50dp" app:layout_constraintBottom_toTopOf="@+id/startbutton" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> </Chronometer> <Button android:id="@+id/startbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:onClick="startStopChronometer" android:text="Start" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/time" /> </android.support.constraint.ConstraintLayout> |
Output:
Finally, run this project.
This is how basic Stopwatch in android looks like.