3

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()
    }
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
BraveEvidence
  • 53
  • 11
  • 45
  • 119
  • 1
    calendar does not recognize that you are in july 15th. So create an instance and store the new value and then use that value while using back or forward. – sanjeev Aug 10 '18 at 14:09
  • 1
    @sanjeev i did what you said but i am getting illegal arguments at cal.add(cal2.get(Calendar.DAY_OF_MONTH),-1) – BraveEvidence Aug 10 '18 at 14:25
  • set the selected date to the place Calendar.DAY_OF_MONTH. That is cal.add(selecteddate,-1) – sanjeev Aug 10 '18 at 14:34
  • Is counter getting set to the correct value? What does the logging show you? You might as well log the counter in the click listeners as well. Does `onDateSelected` get triggered during scrolling or only if they tap on a date? – Geoffrey Wiseman Aug 13 '18 at 14:46
  • counter gets incement or decreemented multiple times even if i swipe a single date – BraveEvidence Aug 13 '18 at 14:48

3 Answers3

0

It looks like it's possible that onDateSelected will get fired during scroll and that you can use that plus a simple +1/-1, but you'll have to try that to see if it works -- it's hard to validate just based on looking at the code for the library.

halfer
  • 19,824
  • 17
  • 99
  • 186
Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
  • 2
    I did what you said, though the error is gone but the behaviour is not correct – BraveEvidence Aug 11 '18 at 05:11
  • Well, progress -- did you change the second parameter? What is happening now instead? – Geoffrey Wiseman Aug 13 '18 at 14:39
  • I updated the code according to bernlim answer but still it does not works. – BraveEvidence Aug 13 '18 at 14:44
  • @Geoffrey, for future reference: if you are certain a question has been changed so substantively that it invalidates existing answers, you may roll it back to a prior state if you wish. We're very keen on discouraging time-wasting questions here. – halfer Aug 13 '18 at 22:05
0

There's a solution with the code you have provided. Try keeping a global counter of the number of swipes (-1 for a back swipe and +1 for a forward swipe). Every time a swipe event happens, update that counter.

Then, in your back and forward listeners, utilize that counter. For example, if you had swiped back 5 days, COUNTER = -5. Then if the user clicks the back button,

cal.add(Calendar.DAY_OF_MONTH, (COUNTER-1))

Remember to reset your counter to 0 every time after a button click.

To combine that with onCalendarScroll, you can set another global variable that tracks the total dx change. When the dx hits a certain value, you know that a full swipe has been made, so the counter +/- 1. Here's some code to illustrate:

private var dx_tracker: Int = 0
private var counter: Int = 0
private SWIPE_VAL = <your tested value>

override fun onCalendarScroll(calendarView: HorizontalCalendarView,
                                  dx: Int, dy: Int) {
    Log.i("Pritish", dx.toString() + dy.toString())
    dx_tracker += dx

    if(dx_tracker >= SWIPE_VAL) {
        counter += 1;
        dx_tracker = 0;
    }
    else if(dx_tracker <= -SWIPE_VAL) {
        counter -= 1;
        dx_tracker = 0;
    }
}
bunbun
  • 2,595
  • 3
  • 34
  • 52
  • 1
    Ah you're using a library from someone else. have you tried using the 'onCalendarScroll' listener mentioned on that library's README? – bunbun Aug 13 '18 at 07:14
  • yes i am using that but dont know how to check for directions. I am checking dx and dy values, scrolling just once, increases or decreases the counter value many times – BraveEvidence Aug 13 '18 at 07:17
  • what if the user scrolls rapidly then too the counter will decrement once and not by the number of dates scrolled by the user – BraveEvidence Aug 13 '18 at 08:05
  • Maybe when dx changes by a certain amount then that will count as "one swipe" or "one date decrement". That is something you will have to test out. But I think with some refining, this idea will work. – bunbun Aug 13 '18 at 08:16
  • could you update your original post with the new code? – bunbun Aug 13 '18 at 08:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177987/discussion-between-bernlim-and-nudge). – bunbun Aug 14 '18 at 03:26
0

You need to set date in cal instance.

I didn't find any syntax which set cal instance.

you have defined cal instance like this

val cal = Calendar.getInstance() 

Your counter is not increasing. So, it will take you to previous date. cal always stores 10th August, If you have not pressed any buttons.

You can reset cal instance onDateSelected method

Lalit Jadav
  • 1,409
  • 1
  • 15
  • 38