Issue subtracting days from calendar

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Issue subtracting days from calendar



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()







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 at 14:09





@sanjeev i did what you said but i am getting illegal arguments at cal.add(cal2.get(Calendar.DAY_OF_MONTH),-1)
– Nudge
Aug 10 at 14:25





set the selected date to the place Calendar.DAY_OF_MONTH. That is cal.add(selecteddate,-1)
– sanjeev
Aug 10 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 at 14:46



onDateSelected





counter gets incement or decreemented multiple times even if i swipe a single date
– Nudge
Aug 13 at 14:48




3 Answers
3



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.


onDateSelected





I did what you said, though the error is gone but the behaviour is not correct
– Nudge
Aug 11 at 5:11





Well, progress -- did you change the second parameter? What is happening now instead?
– Geoffrey Wiseman
Aug 13 at 14:39






I updated the code according to bernlim answer but still it does not works.
– Nudge
Aug 13 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 at 22:05




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,


COUNTER = -5



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


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:


onCalendarScroll


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;






how to know whether user swiped left or right?
– Nudge
Aug 13 at 7:04





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 at 7: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
– Nudge
Aug 13 at 7: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
– Nudge
Aug 13 at 8: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 at 8:16




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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard