I have an horizontal calendar in my application. So I have implemented the following library https://github.com/Mulham-Raee/Horizontal-Calendar I also have implemented my own back and forward button.
Pressing back takes back one day from current date and forward takes one day forward. Suppose today is 10th August and I scrolled horizontally till 15th July without pressing back or forward button. Now if I press back button instead of going to 14th July, I go back to 9th August.
Following is my code
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.myapplication.HorizontalCalendarView
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:selectedDateBackground="@drawable/selected_date"
/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="Back"
app:layout_constraintBottom_toTopOf="@+id/calendarView"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:text="Forward"
app:layout_constraintBottom_toTopOf="@+id/calendarView"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
Kotlin code:
class MainActivity : AppCompatActivity() {
private var counter: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_2)
val format1 = SimpleDateFormat("yyyy-MM-dd", Locale.US)
val currentDate = format1.format(Calendar.getInstance().time)
Toast.makeText(this,currentDate.toString(),Toast.LENGTH_LONG).show()
val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -10)
val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 30)
val horizontalCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(7)
.build()
horizontalCalendar.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {
val selectedDate = format1.format(date.time)
Toast.makeText(this@MainActivity,
"Pritish"+SimpleDateFormat("MMMM yyyy", Locale.US).format(date.time),
Toast.LENGTH_SHORT).show()
if(format1.parse(selectedDate).before(format1.parse(currentDate))){
Toast.makeText(this@MainActivity,"Cannot selecte previous date",Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this@MainActivity,
selectedDate
,Toast.LENGTH_LONG).show()
}
}
override fun onCalendarScroll(calendarView: HorizontalCalendarView,
dx: Int, dy: Int) {
Log.i("Pritish",dx.toString()+dy.toString())
counter += if(dx<0){
-1
} else {
1
}
}
override fun onDateLongClicked(date: Calendar, position: Int): Boolean {
return true
}
})
val cal = Calendar.getInstance()
back.setOnClickListener {
cal.add(Calendar.DAY_OF_MONTH,counter-1)
counter = 0
horizontalCalendar.selectDate(cal,false)
horizontalCalendar.refresh()
}
forward.setOnClickListener {
cal.add(Calendar.DAY_OF_MONTH,counter+1)
counter = 0
horizontalCalendar.selectDate(cal,false)
horizontalCalendar.refresh()
}
}
}