Click here to Skip to main content
13,899,346 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 27 Jun 2017
Licenced CPOL

A Form Generator for Android

, 27 Jun 2017
Rate this:
Please Sign up or sign in to vote.
Form generator is a class module written in Basic4Android (B4A) for build and handle forms data.


This article deals with a class module (fgen) for generating forms on mobile devices with Android Operating system; the language of class is B4A a Basic dialect that must be compiled to obtain a Java executable.

I hope this article will also be useful to those who are not particularly trained on Android where the programs are build how a set of functions that are activated when certain events occur.


In Android, user interaction objects are called views and are placed on the screen by program; if one develops by B4A he can also use a built-in UI, Designer, that can provide declaration of the view and methods for manage the principal events.

Because forms are usually not complex, the class module fgen creates a form from a list of views adding the Ok, Cancel, and Reset buttons. When the form is submitted (by the Ok button) the user receives the data on a data structure (map); only this would not be particularly interesting, but fgen has some other options that make it more useful.

Using the code

The generation of the form is based on a list of views where every view is an ordered set of attributes of which Type and Name are mandatory and Label, Length of view and Default value are possibly. In the list it is possible insert also some other information named (pseudo types).

This article is not a user guide for the software, for this one can download a sample with the documentation, but simply an overview of some functionality built over standard view in order to improve their behaviour.

The B4A project must include the fgen class script, for handle a form the user must:

  • create a list of views,
  • create one instance of the class: Dim fg As fgen,
  • initialize the instance: fg.Initialize,
  • call a function which generates the form,
  • manage the data.

The parameters of the sub form generator: fg.fg(Activity,SectionsParm,Me,"handleAnswer","") are:

  • the activity, this is like a Windows Form,
  • the list of views,
  • the module that contains the subs to handle the data, usually is Me,
  • the name of sub that will receive the data,
  • the possible name of sub for manage the events (explained later with an example),
Sub Globals
Dim SectionsParm As String = "" _
	& "Ground,FF0000FF FF000000;" _
	& "U,TimeStamp,,," & DateTime.Date(DateTime.Now) & ";" _      ' a time stamp is a field not modifiable
	& "Section,Registry;" _
	& "T,Name,,15,El Condor,Patient Name;" _
	& "R,Sex,,12,Man,M:Man|W:Woman;" _
	& "N,Age,,5,75,age,Patient age;" _
	& "R,AgeType,,12,Years,Y:Years|M:Months;" _
	& "CMX,Symptoms,,200,,fever|rush|vomit;" _                     ' a text field which can be compiled also by choice
	& "Required,Name,Sex,Age,Symptoms;" _                          ' some fields are mandatory
	& "After,AgeType,Age;" _                                       ' the radio buttons post after age field
	& "QS,Urgency,,9,Green,White|Green|Yellow|Red;" _              ' qualitative Seek bar
	Dim fg As fgen
End Sub

Sub Activity_Create(FirstTime As Boolean)
fg.Initialize	' instantiate Form Generator class
End Sub
Sub handleAnswer(fh_Data As Map)
	Dim btn As String = fh_Data.Get("fh_button")
	If btn <> "Cancel" Then
	End If
End Sub

The views

Texts and spinner

In addition to normal text and password are provided numeric fields (with or without sign) and real numbers. The set of text fields is completed by hidden fields and not modifiable fields. There are two variants of the spinner with text that is a text where it is possible to write or withdraw an item from a spinner and a spinner with a writeable text where every choice in spinner is recorded in the text, this is useful, for example, to compile a list of symptoms.

Seek bars

The B4A seek bars can have a range from 0 to a positive arbitrary value, in the seek bars of fgen the limits can be any, also negative; the current value is displayed to the right of the seek bar.
The qualitative seek bar is a variant of seek bar where the answer is textual value.
S,Seekbar1,Seek 1,6,-3,10 -10; Seek bar where the range is from 10 to -10 and default value is -3
S,Seekbar2,,6; Seek bar where the range is from 0 to 100 and no default value
QS,Urgency,,9,Green,White|Green|Yellow|Red; Qualitative Seek bar with default value Green

The presentation

The form is presented as a column of view in the order in which they are written, however, a minimum of aesthetic effects are possible by pseudo type Title and Ground; this last sets the background form with color or gradients and a possibly transparency level. The pseudo field After is useful for insert button or check box or radio buttons at right of a view.

Another form of customization is to change the caption of button by image or simply by appropriate Unicode character like ✎ (Chr(‭9998‬)), ✘ (Chr(10008)), ✓ (Chr(‭10003‬‬)), ✉ (Chr(‭‭9993‬)).

One can further customize the form in a sub that receives the generated events; in the fragment below there is an example.

Sub Globals
	Dim sample As String = "TITLE,Title,Test some handle;" _
						& "Ground,FF0000FF FF000000;" _
				        & "QS,Urgency,,9,Green,White|Green|Yellow|Red;" _
						& "T,Mail,,18,,insert mail,mai required;" _
						& "Check,Mail is mail;" _
						& "Required,Mail;" _
						& "CKB,Consent,,,, to keep data;" _
						& "N,Number,,10,,tell me,Insert Number;" _
						& "P,psw,Password,15,Insert password,Almost 6 characters;" _
						& "U,UnMod,Date,,__________________________;" _
						& "B,BtnGo,Go,,disabled;" _
						& "After,Consent,Mail"
	Dim pnl As Panel
	Dim fg As fgen	' instantiate Form Generator class
End Sub
Sub Activity_Create(FirstTime As Boolean)
	If Not(fg.IsInitialized()) Then fg.Initialize
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub handleAnswer(fh_Data As Map)
	Msgbox(fh_Data.Get("fh_button"),"Button pressed")
End Sub
Sub handleEvents(parm() As Object)	As Boolean	' view events handler
	' receive view, event, possible value
	Dim value As String = ""
	If parm.Length > 2 Then
		If parm.Length > 3 Then value = parm(3)
	End If
	Log("Handle event " & parm(0) & " event: " & parm(1) & " Value: " & value)
	If parm(1) = "Start" Then
		Dim lbl As Label = fg.getHandle("Title")
		lbl.TextColor = Colors.Red
		lbl.Color = Colors.Green
		lbl = fg.getHandle("UnMod")
		DateTime.DateFormat = "d MMMM y"	' e.g. 24 June 2017
		lbl.Text = DateTime.Date(DateTime.Now)
		Select parm(0)
			Case "Consent"
				If parm(3) = 1 Then fg.enable("BtnGo")
			Case "Urgency"
				Private urgencyColors() As Int = Array As Int(Colors.White,Colors.Green,Colors.Yellow,Colors.Red)
				Private txt As EditText = parm(4)
				txt.TextColor = urgencyColors(parm(3))		' set color
		End Select
	End If
	Return True
End Sub

Some detail and explanation: the sub handleEvents receive the control when some common event are fired, the parameter of sub is an array containing the view name, the event and the possible value. In the fragment above at the start event (If parm(1) = "Start" Then) the background and color of Title are changed and the field UnMod receive the date.

When the event occurs in check box Consent the button BtnGo is enabled or disabled depending on the check box; finally the text on qualitative seek bar Urgency assumes the relative color.

Multi form

Mobile screen size makes form handling difficult and, although fgen generates a scroll bar when needed, it allows to divide a complex form into sections by the pseudo type Section and adding appropriate <-- backward and --> forward buttons to the form.

Views or pseudo types (as Ground pseudo type) commons to all sections must appears before the first section.

Un alternative is a pseudo type TAB that allows to organize the form through a tabHost; it can be used in sections. Before the first tab are allowed only ground and title pseudo type.

Check data and input tips

fgen provides some controls on data via the pseudo types Required and Check; Check performs field controls through comparison operators or by regular expressions; an alert can be provided. The text fields can have two extra attributes, the first is used as hint, the second generated a toast message. For example, a message that appears for a while when the field gets the focus.

& "Section,Password,AgeType=Y,Age>17;" _                        ' This session is shown if the condition is satisfied
& "P,psw,Password,15,,Insert password,almost 6 characters;" _   ' example of hint and toast message
& "P,repeatPsw,Repeat Password,15,,Retype password;" _
& "Required,psw;" _                                             ' password must be entered
& "Check,psw=repeatPsw,Password unmatched;" _                   ' password must match with the repetition
& "Check,psw is .{6" & Chr(1) & "},Password too short;" _       ' password must be almost 6 characters long regular expression .{6,}
& "Section,VitalParameters;" _
& "S,Height,,6,,0 220;" _
& "S,Weight,,6,,0 130;" _
& "S,Min,Minimum,6,,20 200;" _
& "S,Max,Maximum,6,,40 280;" _
& "S,HeartRate,Heart rate,6,,0 250;" _
& "S,Temperature,,6,36.5,30 48;" _
& "S,Saturation,,6,,0 100;" _
& "Check,Max>Min,Max Pressure must be greater of min pressure;" ' coherence control from min ad max blood pressure

Install the sample

The formgen.apk found in the .zip file must be copied on the mobile. See the article on Stack Overflow: How to install apk from PC?


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Member 4206974
Software Developer Condor Informatique
Italy Italy
Computer literacy (software) : Languages: PHP, Javascript, SQL Autoit,Basic4Android; Frameworks: JOOMLA!
Teaching/Training skills on Office, WEB site development and programming languages.
Others : WEB site development.
UNDP Missions
feb – may 2003 Congo DR Bukavu: ground IT computer course
nov 2003 Burundi Bujumbura: Oracle Data Base course
feb 2005 Burundi Bujumbura: JAVA course
mar 2005 Mali Kati: MS Office course
oct 2006 Mali Kati: MS Office course
jun 2006 Burkina Faso Bobo Dioulasso: MS Office course
jun 2007 Burkina Faso Bobo Dioulasso: MS Office course
may 2007 Argentina Olavarria hospital: Internet application for access to medical records
apr 2008 Burkina Faso Ouagadougou: MS ACCESS and dynamic Internet applications
jun 2008 Niger Niamey: analysis of the computing needs of the Niamey hospital
may 2009 Burkina Faso Ouagadougou: MS ACCESS and dynamic Internet applications
oct 2010 Niger Niamey: analysis of the computing needs of the Niamey hospital (following)
Region Piedmont project Evaluation
mar 2006 Burkina Faso, Niger
mar 2007 Benin, Burkina Faso, Niger
sep 2008 Benin, Burkina Faso, Niger
feb 2010 Burundi Kiremba hospital: MS Office course
feb 2011 Congo DR Kampene hospital: MS Office course

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 27 Jun 2017
Article Copyright 2017 by Member 4206974
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid