With this library you can pick a date or time for Android, with support Gregorian and Jalali calendar in the same time. this library is based on the main library (https://github.com/wdullaer/MaterialDateTimePicker) and only the persian jalali calendar and custom font added to it.
Just add this line to your build.gradle
dependencies :
dependencies {
implementation 'ir.scriptestan.jalalimaterialdatetimepicker:library:0.1.2'
}
Gregorian | Persian Jalali |
---|---|
class MainActivity : AppCompatActivity(), DatePickerDialog.OnDateSetListener {
var dpd: DatePickerDialog? = null
// Change this
var calendarType: DatePickerDialog.Type = DatePickerDialog.Type.JALALI // or DatePickerDialog.Type.GREGORIAN
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val now: Calendar? = when (calendarType) {
DatePickerDialog.Type.GREGORIAN -> Calendar.getInstance()
DatePickerDialog.Type.JALALI -> JalaliCalendar.getInstance()
}
/**
* Date picker setup
*/
if (dpd == null) {
dpd = DatePickerDialog.newInstance(
calendarType,
this,
now!!.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
)
} else {
dpd!!.calendarType = calendarType
dpd!!.initialize(
this,
now!!.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
)
}
/**
* For setting font
*
when (dpd?.calendarType) {
DatePickerDialog.Type.GREGORIAN -> dpd!!.setFont(null)
DatePickerDialog.Type.JALALI -> {
dpd!!.setFont(Typeface.createFromAsset(assets, "IRANSansMobile(FaNum).ttf"))
}
}
*/
/**
* Showing date picker
*/
dpd!!.show(supportFragmentManager, "DatePickerDialog")
}
}
override fun onDateSet(view: DatePickerDialog?, year: Int, monthOfYear: Int, dayOfMonth: Int) {
Toast.makeText(this,
"Picked date: $dayOfMonth/${monthOfYear+1}/$year", Toast.LENGTH_LONG).show()
}
}
for more options see sample folder or main library readme