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()
        }

    }
}


Saturday, August 22, 2020

Simple example using TextClock with Kotlin

TextClock can display the current date and/or time as a formatted string. It's a simple example of using TextClock with Kotlin.

layout XML with TextClock

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextClock
        android:id="@+id/mytextclock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic|bold"
        android:textSize="40dp"
        android:format12Hour="hh:mm:ss a"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Access TextClock using Kotlin

package com.blogspot.android_er.androidtextclock

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextClock
import android.widget.Toast

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

        val myTextClock = findViewById<TextClock>(R.id.mytextclock)
        myTextClock.setOnClickListener{
            val s = myTextClock.text
            Toast.makeText(this, s, Toast.LENGTH_LONG).show()
        }

    }
}


Friday, August 21, 2020

Get Android and SDK version using Kotlin

 Exercise to get Android device info and compile SDK version using Kotlin.

Run on Android Emulator of Android 10

Run on Redmi 5 Plus (Android 8.1.0)

MainActivity.kt

package com.blogspot.android_er.androidinfo

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.annotation.RequiresApi

class MainActivity : AppCompatActivity() {

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

        val manufacturer = Build.MANUFACTURER
        val model = Build.MODEL
        val release = Build.VERSION.RELEASE

        val pkgInfo = packageManager.getPackageInfo(packageName, 0)
        val appInfo = pkgInfo.applicationInfo
        val targetSdkVersion = appInfo.targetSdkVersion
        val minSdkVersion = appInfo.minSdkVersion

        //Display system and SDK info for reference
        val tvInfo = findViewById<TextView>(R.id.info)
        tvInfo.setText(manufacturer + "\n"  +
                model + "\n" +
                "Android: " + release + "\n" +
                "targetSdkVersion: " + targetSdkVersion + "\n" +
                "minSdkVersion: " + minSdkVersion)
    }
}

layout xml

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android-er"
        android:textStyle="bold"
        android:textSize="34dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-er.blogspot.com   " />
    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="30dp"/>

</LinearLayout>