MainActivity.kt
package com.cfsuman.kotlintutorials
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets reference from XML layout
val textView = findViewById<TextView>(R.id.textView)
val timePicker = findViewById<TimePicker>(R.id.timePicker)
val buttonGet = findViewById<Button>(R.id.buttonGet)
val buttonSet = findViewById<Button>(R.id.buttonSet)
val buttonToggle = findViewById<Button>(R.id.buttonToggle)
// Set a time change listener for time picker widget
timePicker.setOnTimeChangedListener{
view,hourOfDay,minute->
textView.text = "Time Changed ${getHourAMPM(hourOfDay)} " +
": $minute ${getAMPM(hourOfDay)}"
}
// Set a random time
buttonSet.setOnClickListener{
// Set a random time
setPickerTime(timePicker)
}
// Set a click listener for get time button widget
buttonGet.setOnClickListener{
// Display the picker current time on text view
textView.text = "Selected Time : ${getPickerTime(timePicker)}"
}
// Toggle time picker 24 hour view
buttonToggle.setOnClickListener{
if(timePicker.is24HourView){
timePicker.setIs24HourView(false)
}else{
timePicker.setIs24HourView(true)
}
}
}
// Get time picker current time as string
private fun getPickerTime(timePicker: TimePicker):String{
val hour = timePicker.hour
val minute = timePicker.minute
return "${getHourAMPM(hour)} : $minute ${getAMPM(hour)}"
}
// Get AM PM value from provided hour
private fun getAMPM(hour:Int):String{
return if(hour>11)"PM" else "AM"
}
// Get hour for AM PM time format
private fun getHourAMPM(hour:Int):Int{
// Return the hour value for AM PM time format
var modifiedHour = if (hour>11)hour-12 else hour
if (modifiedHour == 0){modifiedHour = 12}
return modifiedHour
}
// Set time picker random time
private fun setPickerTime(timePicker: TimePicker){
// Get random time
// Kotlin random range(inclusive,exclusive)
val hour = Random.nextInt(0,24)
val minute = Random.nextInt(0,60)
timePicker.hour = hour
timePicker.minute = minute
}
}
activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#DCDCDC"
android:padding="24dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#E6E5E5"
android:fontFamily="sans-serif"
android:textSize="20sp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:timePickerMode="spinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/buttonGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Get Time"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timePicker" />
<Button
android:id="@+id/buttonSet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Set Time"
app:layout_constraintBottom_toBottomOf="@+id/buttonGet"
app:layout_constraintStart_toEndOf="@+id/buttonGet" />
<Button
android:id="@+id/buttonToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Toggle 24"
app:layout_constraintBottom_toBottomOf="@+id/buttonSet"
app:layout_constraintStart_toEndOf="@+id/buttonSet" />
</androidx.constraintlayout.widget.ConstraintLayout>