Last active
October 21, 2022 09:00
-
-
Save Kaspic/ffdf5022ef0ac14daa40820f3ff8e67f to your computer and use it in GitHub Desktop.
Android MaterialDatePicker, MaterialTimePicker tutorial - https://kennay-kermani.medium.com/android-material-date-picker-and-material-time-picker-in-practice-11c01582e52e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<TextView | |
android:id="@+id/output" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="--" | |
android:textSize="20sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<com.google.android.material.button.MaterialButton | |
android:id="@+id/select_button" | |
android:layout_width="wrap_content" | |
android:layout_height="0dp" | |
android:text="Select" | |
android:layout_marginTop="16dp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/output" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.google.android.material:material:1.3.0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.TextView | |
import androidx.core.util.Pair | |
import com.google.android.material.button.MaterialButton | |
import com.google.android.material.datepicker.MaterialDatePicker | |
import java.time.Instant | |
import java.time.LocalDateTime | |
import java.time.ZoneId | |
import java.time.format.DateTimeFormatter | |
class MainActivity : AppCompatActivity() { | |
private var currentSelectedStartDate: Long? = null | |
private var currentSelectedEndDate: Long? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setClickListeners() | |
} | |
private fun setClickListeners() { | |
findViewById<MaterialButton>(R.id.select_button).setOnClickListener { showDatePicker() } | |
} | |
private fun showDatePicker() { | |
val selectedStartDateInMillis = currentSelectedStartDate ?: System.currentTimeMillis() | |
val selectedEndDateInMillis = currentSelectedEndDate ?: System.currentTimeMillis() | |
val dateRange = androidx.core.util.Pair(selectedStartDateInMillis, selectedEndDateInMillis) | |
MaterialDatePicker.Builder.dateRangePicker().setSelection(dateRange).build().apply { | |
addOnPositiveButtonClickListener { selectedDateRangePair -> onDateRangeSelected(selectedDateRangePair) } | |
}.show(supportFragmentManager, MaterialDatePicker::class.java.canonicalName) | |
} | |
private fun onDateRangeSelected(dateTimeStampInMillis: Pair<Long, Long>) { | |
currentSelectedStartDate = dateTimeStampInMillis.first ?: System.currentTimeMillis() | |
currentSelectedEndDate = dateTimeStampInMillis.second ?: System.currentTimeMillis() | |
val startDateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentSelectedStartDate), ZoneId.systemDefault()) | |
val endDateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentSelectedEndDate), ZoneId.systemDefault()) | |
val datePattern = "yyyy-MM-dd" | |
val startDateFormatted: String = startDateTime.format(DateTimeFormatter.ofPattern(datePattern)) | |
val endDateFormatted: String = endDateTime.format(DateTimeFormatter.ofPattern(datePattern)) | |
"$startDateFormatted -- $endDateFormatted".also { findViewById<TextView>(R.id.output).text = it } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.TextView | |
import com.google.android.material.button.MaterialButton | |
import com.google.android.material.datepicker.MaterialDatePicker | |
import java.time.Instant | |
import java.time.LocalDateTime | |
import java.time.ZoneId | |
import java.time.format.DateTimeFormatter | |
class MainActivity : AppCompatActivity() { | |
private var currentSelectedDate: Long? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setClickListeners() | |
} | |
private fun setClickListeners() { | |
findViewById<MaterialButton>(R.id.select_button).setOnClickListener { showDatePicker() } | |
} | |
private fun showDatePicker() { | |
val selectedDateInMillis = currentSelectedDate ?: System.currentTimeMillis() | |
MaterialDatePicker.Builder.datePicker().setSelection(selectedDateInMillis).build().apply { | |
addOnPositiveButtonClickListener { dateInMillis -> onDateSelected(dateInMillis) } | |
}.show(supportFragmentManager, MaterialDatePicker::class.java.canonicalName) | |
} | |
private fun onDateSelected(dateTimeStampInMillis: Long) { | |
currentSelectedDate = dateTimeStampInMillis | |
val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentSelectedDate), ZoneId.systemDefault()) | |
val dateAsFormattedText: String = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) | |
findViewById<TextView>(R.id.output).text = dateAsFormattedText | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val usesSystem24HFormat = is24HourFormat(this) | |
val timeFormat = if (usesSystem24HFormat) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.setInputMode(INPUT_MODE_KEYBOARD) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle | |
import android.widget.TextView | |
import androidx.appcompat.app.AppCompatActivity | |
import com.google.android.material.button.MaterialButton | |
import com.google.android.material.timepicker.MaterialTimePicker | |
import com.google.android.material.timepicker.TimeFormat | |
import java.time.LocalDateTime | |
class MainActivity : AppCompatActivity() { | |
private var selectedHour: Int? = null | |
private var selectedMinute: Int? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setClickListeners() | |
} | |
private fun setClickListeners() { | |
findViewById<MaterialButton>(R.id.select_button).setOnClickListener { showTimePicker() } | |
} | |
private fun showTimePicker() { | |
val hour = selectedHour ?: LocalDateTime.now().hour | |
val minute = selectedMinute ?: LocalDateTime.now().minute | |
MaterialTimePicker.Builder() | |
.setTimeFormat(TimeFormat.CLOCK_24H) | |
.setHour(hour) | |
.setMinute(minute) | |
.build() | |
.apply { | |
addOnPositiveButtonClickListener { onTimeSelected(this.hour, this.minute) } | |
}.show(supportFragmentManager, MaterialTimePicker::class.java.canonicalName) | |
} | |
private fun onTimeSelected(hour: Int, minute: Int) { | |
selectedHour = hour | |
selectedMinute = minute | |
val hourAsText = if (hour < 10) "0$hour" else hour | |
val minuteAsText = if (minute < 10) "0$minute" else minute | |
"$hourAsText:$minuteAsText".also { findViewById<TextView>(R.id.output).text = it } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment