Cursussen/Courses Codesnippets     Top 
B4A-calendar - Layout adjustments


1. Fonts
Lets do some work on the layout by adding some color, changing the fonts and giving the boxes a border.
We can do most of it in the set_label subroutine. Make the following changes:
private Sub set_label(lbltext As String) As Label
	Private lbl As Label
	lbl.Initialize("")
	lbl.Typeface = Typeface.DEFAULT_BOLD
	lbl.TextSize = 16
	lbl.Text = lbltext
	lbl.Gravity = Bit.Or(Gravity.CENTER_HORIZONTAL,Gravity.CENTER_VERTICAL)
	Return lbl
End Sub
Now the text looks better.


2. Colors
On to some coloring. Add a subroutine set_background and give it an argument for the color integer.
Private Sub set_background(intcolor As Int) As StateListDrawable
	Private cd,cd2 As ColorDrawable
	Private sd As StateListDrawable
	cd.Initialize2(intcolor,5,1,Colors.Black)
	cd2.Initialize2(Colors.Red,5,1,Colors.Black)
	sd.Initialize
	sd.AddState(sd.State_Pressed,cd2)
	sd.AddState(sd.State_Enabled,cd)
	Return sd
End Sub
In the set_label subroutine you can now give the background of the label a color. Add the following line of code:
lbl.Background = set_background(intbackground) Change the arguments of the set_label subroutine to include the intbackground variable.
private Sub set_label(lbltext As String, intbackground As Int) As Label
In the calls to the set_label subroutine you need to add a color. You can use the standard colors or you can specify the color with the Colors.ARGB method.
set_label(mnth & " / " & yr, Colors.ARGB(100,182,221,249))
set_label(lstdaynames.Get(i-1), Colors.ARGB(100,255,255,135))
set_label("",Colors.ARGB(100,230,230,230))
set_label("", Colors.ARGB(100,230,230,230))
set_label(daynumber, Colors.ARGB(100,255,50,50))
set_label(daynumber, Colors.ARGB(100,255,210,179))
set_label(daynumber,Colors.ARGB(100,158,255,150))
Now the calendar app looks like this:


3. Month name
For the month/year label you can use a months list to get the right month name.
Private lstmonthnames As List = Array As String("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
Private monthname As String = lstmonthnames.Get(mnth-1)
And adjust the call to set_label.


4. Screensize
One final layout adjustment could be to have the calendar take up the whole screen.
Add the following lines below the declarations of boxwidth and boxheight:
' screen adjustments
Dim screensize As Double = GetDeviceLayoutValues.ApproximateScreenSize
Dim devwidth As Int = GetDeviceLayoutValues.Width/2
Dim devheight As Int = (GetDeviceLayoutValues.height/2) - 40dip	 ' title bar height not included
If screensize > 8 Then
	Dim devwidth As Int = GetDeviceLayoutValues.Width
	Dim devheight As Int = GetDeviceLayoutValues.height - 80dip	 ' title bar height not included	
End If
Private boxwidth As Int = DipToCurrent(devwidth/7)
Private boxheight As Int = DipToCurrent(devheight/8)
Now the boxes become bigger (this also works on a tablet with a screen size above 8!)