B4A Library Custom Calendar Class

Similar threads

B4A Code Snippet [B4X] Light theme B4XDateTemplate
B4A Example B4Acalendar
Other [new feature] Auto backups
B4A Code Snippet Theme Colors
B4A Library Calendar Library

Takeru17

Member
Licensed User
Hi,
This is my first contribution to B4A.
It's a very simple calendar clickable.

Instructions are :

Initialize(Callback, EventName, Width (same declare as addview), Height (idem), date (DateTimeFormat) )

ShowCalendar(Boolean)
LengthMonth(Year, Month) As Int
SetTableColor(Color) : color of the grid
SetBackGroundColor(Color) : color of the Background
SetActiveButtonColor(Color) : color of calendar's buttons
SetInactiveButtonColor(Color) : color of day's buttons
SetFunctionButtonColor(Color) : color of PrevMonth, NextMonth, Years Buttons
SetTextSize(value) : relative Size for Texts

NmFullday(0..6) : name each days
Nmday(0..6) : name (on 3 letters) for days
NmFullMonth(0..12) :name each months
NmMonth(0..11) : name (on 3 letters) for months
(theses tables are in French actually)
 

Attachments

Last edited:

StarinschiAndrei

Active Member
Licensed User
Hi ,

i try to use your cutom calendar but i got an erroe message:
"SomeTime=DateTime.DateParse(CallMonth&"/01/"&calYear)"
java.text.ParseException:unparsable date:"12/01/2012"(at offset 2)

it happen only if i call second time from same button your calendar.
Sub btnscadenta_Click
If btnscadenta.Text ="Urmatoarea scadenta :" Then
CsCal.Initialize(Me,"CsCal",100%x,100%y,DateTime.Now)
Activity.AddView(CsCal.AsView,0,0,100%x,100%y)
CsCal.ShowCalendar(True)
lansare="scadenta"
End If
End Sub
Could you please tell me why it happen ?
Thank you.
 

Harris

Expert
Licensed User
New Version - modified original

I liked this simple/easy calendar class so much, I modified it.

1. Added the AHLocale lib to supply the lists of weekday, short and long month names. Now supports any language on a device. I recommend everyone use this for these purposes ( hey - why not?)

2. Restricted day selection to the current month in view.

3. Title shows the day selected. Added Sub BtJour_Down event to accomplish this.

4. Added _ItemLongClick event (removed short click) to make the actual day selection.

5. Created Private vars for Start and End years used in spinner view. Set the begin and end years in one place to whatever you need.


Further mods: Could use bigger font size on lager devices (tablets)...


Thanks to the author Takeru17 for this class and inspiration.

Tested on Nexus 7 tablet and Samsung S phone.

Cheers
 

Attachments

Harris

Expert
Licensed User
Got me beat...

This is how the cal looks on a SG2 phone in landscape (See Attached).

My Galaxy S has 206 ppi. Cal looks fine in landscape
Galaxy S2 has 240.

Why does S2 cut off half the date number?

Both look fine in portrait.
 

Attachments

Last edited:

Cableguy

Expert
Licensed User
I can point 2 reasons, and they may both occur simultaneously...

. The 'S2' screen resolution being different from the 'S'
. The Font is too big for the label size, which seem to be much smaller than the outlined box around it...

For the first case, you must use some code to first establish the screen resolution, set it to a var and use it in the calculations for the sizing...
For the second, widen up the label to the box size -1, play a bit with the font size and with theppositioning, try to align up vertically.
 

JTmartins

Active Member
Licensed User
How can I Move and change font in YEAR

Hi Takeru17,

I've played with your class with diferent font sizes and colors, just one thing I can't catch.

How can we center the year and change the font to bold...?

Everyhting else is a button or a label, but this one is a spinner, right ?

Just because in my opinion it does not look right there on to left.

I've tryed, but couldn't find a way.

Thanks,

José
 

Harris

Expert
Licensed User
"How can we center the year and change the font to bold...?"

Check my post above - "New Version - modified original"

In the supplied modified class (and example):
Search for spLabel in the class. It explains how to accomplish what you asked for.

Essentially, the code hides the spinner view and substitutes a label to present the UI, using the spinner list and events.

Who knew that this kind of trickery could be performed (aside from Erel and possibly 1000 others).

Note: Additional libs - AHLocal, Reflection

Cheers
 

Attachments

JTmartins

Active Member
Licensed User
Dates do not change

Hi Harris,

*********************************************************
SORTED OUT...NEVER MIND ..MISSING SUB CsCal_ItemLongClick IN ACTIVITY
*********************************************************

I've tryed to run your example calendar together with AHviewpager Library,
however when I do that, the calendar displays OK in a page but when clicked in the days nothing happens. However the panel with previous month, year, and next month works as expected.

Tried in tablet and emulator.

I had the same problem with sliding menus, when Agenda menu selected clicking in a date does not work, so I presume that something else is catching the events, but do not know how to solve this.

Help, please..

Thanks
José
 
Last edited:

Harris

Expert
Licensed User
Yes,
the comment in CsCal.itemlongclick somewhat gives it away...

' Comment out ALL lines below for production use. Use dt param above (selected date) as you wish...

Glad you figured it out... It gets easier the more we code, study, experiment as well as read posts. Intents and Reflection are still quite puzzling however. Good thing for many examples to "borrow" code to get me thru...
 

JTmartins

Active Member
Licensed User
Why is this ?

I have Custom calendar working ok in a sample project however, when I run it from a button on the final project

I get this

An Error has occured in sub:CustomCalendar_printdate (java line 602)
Java.Text.ParseException:Unparsable date "5/01/2013" (at offset 1)

Any clues ?

Thanks
José

I can see it has something to do with this SOMETIME lines

SomeTime = DateTime.DateParse(CalMonth & "/01/" & CalYear)
 
Last edited:

JTmartins

Active Member
Licensed User
The Value is

Hi Erel,

The value in DateTime.DateFormat is

DateTime.DateFormat="yyyy-MM-dd"

I use it in other part of the app that depend on date and this is the date format in the server.

In the sub of the button that opens the calendar I've changed to
B4X:
DateTime.DateFormat="dd/MM/yyyy"
Now the calendar shows up, but when I click on the date I get

customcalendar_daypress (B4A line: 378)


SomeTime = DateTime.DateParse(mm & "/" & dd & "/" & aa)
java.text.ParseException: Unparseable date: "6/11/2013" (at offset 1)


at java.text.DateFormat.parse(DateFormat.java:626)
at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:147)
at Gesdev4.dev.customcalendar._daypress(customcalendar.java:385)
at Gesdev4.dev.customcalendar._btjour_longclick(customcalendar.java:138)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$2.onLongClick(ViewWrapper.java:73)
at android.view.View.performLongClick(View.java:4120)
at android.widget.TextView.performLongClick(TextView.java:7875)
at android.view.View$CheckForLongPress.run(View.java:16945)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
java.text.ParseException: Unparseable date: "6/11/2013" (at offset 1)

The same happens if I use

B4X:
DateTime.DateFormat="MM/dd/yyyy"
PS - AHlocale in use. I don't know if it has anything to do with that.
 
Last edited:

JTmartins

Active Member
Licensed User
This is my code

In the button sub that calls the calendar this is what I have.

B4X:
Sub btnMarcaAula_Click
' para marcar aula precisamos de pedir
' data
' hora de inicio, hora de fim
' aluno (por LA)
' primeiro esconder todos os paineis
DateTime.DateFormat="MM/dd/yyyy"
Panel1.Visible=False
Panel2.Visible=False
Panel3.Visible=False
Panel4.Visible=False
Panel6.Visible=False
Panel7.Visible=False
Panel8.Visible=False
Panel9.Visible=False
Panel10.Visible=False
PanelLerMsg.Visible=False
Panelmsg.Visible=False
Panel5.Visible=True ' mostra o painel do calendário


CsCal.Initialize(Me,"CsCal",100%x,90%y,DateTime.Now)

Panel5.AddView(CsCal.AsView,0,0,Activity.Width,Activity.height)
CsCal.ShowCalendar(True)


End Sub
Then I have

B4X:
Sub CsCal_ItemLongClick(dt As Long)   

   DateTime.DateFormat="MM/dd/yyyy" ' just to make sure again.
   
CsCal.ShowCalendar(False)


   
   Dim aa,dd,mm As Int
   aa=DateTime.GetYear(dt)
   dd=DateTime.GetDayOfMonth(dt)
   mm=DateTime.GetMonth(dt)
   
   Msgbox( CsCal.NmFullMonth(mm-1) &"  "&dd& "  " & aa," Data escolhida")
    CsCal.ShowCalendar(True)
   
   
End Sub
This is the error i receive when I click on a calendar day , and can't understand why.



java.text.ParseException: Unparseable date: "6/13/2013" (at offset 1)
at java.text.DateFormat.parse(DateFormat.java:626)
at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:147)
at Gesdev4.dev.customcalendar._daypress(customcalendar.java:274)
at Gesdev4.dev.customcalendar._btjour_longclick(customcalendar.java:94)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$2.onLongClick(ViewWrapper.java:73)
at android.view.View.performLongClick(View.java:4120)
at android.widget.TextView.performLongClick(TextView.java:7875)
at android.view.View$CheckForLongPress.run(View.java:16945)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)


at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
java.text.ParseException: Unparseable date: "6/13/2013" (at offset 1)
 

JTmartins

Active Member
Licensed User
It works as an activity

I couldn't find anything wrong in the class.

Running in the same activity from a button gives the error mentioned before.

However clicking on the button, launching a new activity with the calender it works. And I don't even touch the datetime.dateformat.

Not the solution I wanted as reverting to the previous activity is going to be a little complicated, but at least it works.

If someone has a little time, maybe can investigate why this happens, as I really didn't catch the problem.
 
how to make when user click some date, the cell background color changed. so they will know what they click ?

i try
SetActiveButtonColor(Color) : color of calendar's buttons -> all the calendar button becomes that color
SetInactiveButtonColor(Color) : color of day's buttons
 
Last edited:

pesquera

Active Member
Licensed User
Nice job! it was very useful to me..
TsubasaOzora9: I did some changes for that

on Sub PrintDate:
B4X:
'        If (cmm=CalMonth) AND (caa=CalYear) Then
'            If (z=cdd) Then
          If (z=CalDay) Then  ' pesquera
                btDays(i).Color=ColFunction
            End If
'        End If
on Sub DayPress:
B4X:
  PrintDate(SomeTime)  ' pesquera
  Return True
 
Top