Wednesday, August 26, 2020

Chronometer example (Kotlin)

 Android example of using Chronometer (count-up and count-down) widget, with Kotlin

Layout with Chronometer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context=".MainActivity"
    android:layout_margin="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Chronometer Example (Kotlin)"
        android:layout_gravity="center"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android-er.blogspot.com"
        android:layout_gravity="center"/>
    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="34dp"
        android:textStyle="bold"/>
    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start (Count Up)"/>
    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop"/>
    <Button
        android:id="@+id/reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Reset"/>

    <Chronometer
        android:id="@+id/chronometerDn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="34dp"
        android:textStyle="bold"
        android:countDown="true"/>
    <Button
        android:id="@+id/startDn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start (Count Down)"/>
    <Button
        android:id="@+id/stopDn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop"/>
    <Button
        android:id="@+id/resetDn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Reset"/>

</LinearLayout>
Android Kotlin code


package com.blogspot.android_er.androidchronometer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.SystemClock
import android.widget.Button
import android.widget.Chronometer
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val myChronometer = findViewById<Chronometer>(R.id.chronometer)
        val btnStart = findViewById<Button>(R.id.start)
        val btnStop = findViewById<Button>(R.id.stop)
        val btnReset = findViewById<Button>(R.id.reset)

        btnStart.setOnClickListener(){
            myChronometer.start();
            Toast.makeText(this,
                    "myChronometer.start()",
                    Toast.LENGTH_LONG).show()
        }

        btnStop.setOnClickListener(){
            myChronometer.stop();
            Toast.makeText(this,
                    "myChronometer.stop()",
                    Toast.LENGTH_LONG).show()
        }

        btnReset.setOnClickListener(){
            myChronometer.base = SystemClock.elapsedRealtime()
            Toast.makeText(this,
                    "myChronometer.base = SystemClock.elapsedRealtime()",
                    Toast.LENGTH_LONG).show()
        }

        val myChronometerDn = findViewById<Chronometer>(R.id.chronometerDn)
        val btnStartDn = findViewById<Button>(R.id.startDn)
        val btnStopDn = findViewById<Button>(R.id.stopDn)
        val btnResetDn = findViewById<Button>(R.id.resetDn)

        btnStartDn.setOnClickListener(){
            myChronometerDn.base = SystemClock.elapsedRealtime() + 10000
            myChronometerDn.start()
            Toast.makeText(this,
                    "myChronometerDn.base = " +
                            "SystemClock.elapsedRealtime() + 10000 \n" +
                            "myChronometerDn.start()",
                    Toast.LENGTH_LONG).show()
        }

        btnStopDn.setOnClickListener(){
            myChronometerDn.stop()
            Toast.makeText(this,
                    "myChronometerDn.stop()",
                    Toast.LENGTH_LONG).show()
        }

        btnResetDn.setOnClickListener(){
            myChronometerDn.base = SystemClock.elapsedRealtime()
            Toast.makeText(this,
                    "myChronometerDn.base = SystemClock.elapsedRealtime()",
                    Toast.LENGTH_LONG).show()
        }

    }
}


No comments:

Post a Comment