Created
March 6, 2021 10:59
-
-
Save shibbirweb/e9c3f93abd8cd8d9a5d8dd370d520640 to your computer and use it in GitHub Desktop.
Show date picker on textview
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.app.DatePickerDialog | |
| import android.content.Context | |
| import android.widget.TextView | |
| import java.util.* | |
| object DatePickerHelper { | |
| /** | |
| * Show date picker | |
| */ | |
| fun showDatePicker(context: Context, textView: TextView, initialDate: String? = null) { | |
| val calendar = Calendar.getInstance() | |
| var initDay = calendar.get(Calendar.DAY_OF_MONTH) | |
| var initMonth = calendar.get(Calendar.MONTH) | |
| var initYear = calendar.get(Calendar.YEAR) | |
| if (initialDate != null && initialDate.isNotEmpty()) { | |
| val splintedDate = initialDate.split("-") | |
| if (splintedDate.size == 3) { | |
| try { | |
| initYear = splintedDate[0].toInt() | |
| initMonth = splintedDate[1].toInt() - 1 | |
| initDay = splintedDate[2].toInt() | |
| } catch (e: Exception) { | |
| } | |
| } | |
| } | |
| val picker = DatePickerDialog( | |
| context, | |
| { _, year, month, dayOfMonth -> | |
| // display selected date in text view | |
| textView.text = "$year-${month + 1}-$dayOfMonth" | |
| }, | |
| initYear, | |
| initMonth, | |
| initDay | |
| ) | |
| picker.show() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment