Click here to Skip to main content
15,881,281 members
Articles / Mobile Apps / Android

A Form Generator for Android

Rate me:
Please Sign up or sign in to vote.
3.68/5 (4 votes)
22 Oct 2022CPOL6 min read 14.9K   440   6   1
Form generator is a class module written in Basic4Android (B4A) for build and handle forms data.
This article deals with fgen, a class module, for generating forms on mobile devices with Android Operating system; the development language is B4A, a Basic dialect that must be compiled to obtain a Java executable.

Introduction

This article deals with a class module (fgen) for generating forms on mobile devices with Android Operating system; the development language 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 development where the programs are built how a set of functions that are activated when certain events occur.

This is an update of the article that reflects the functional and aesthetic evolution that the tool has acquired over time especially with its use in the development of real applications.
The view added are: Date and Time, Check list, extended toggle buttons, graphic buttons and timer. From a functional and aesthetic point of view, the shape of the buttons and the presentation of comments have been improved, it has also been introduced  the support for FontAwesome and Material Icons fonts, finally, with the pseudo-type Window, the form can be sized and positioned at will on the screen.

Background

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 demonstration program (sandbox) with the documentation, but it is 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 one of the two functions which generates the form
  • manage the data

The parameters of the sub form generator:

VBScript
fg.fg(Activity,parameters,Me,"handleAnswerSub","handleEventsSub")

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).
VBScript
...
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
fg.fg(Activity,SectionsParm,Me,"handleAnswer","")
...
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 second mode for generate the form allows to wait for it to be closed and to continue in the same sub: fg.fgw(Activity,parameters,Me,"handleEventsSub"):

VBScript
fg.fgw(Activity,parameters,Me,"")
Do While fg.fh_isrunning
    Sleep(100)
Loop
If fg.fh_Data.Get("fh_button") <> "Cancel" Then
...

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. In addition to the standard spinner, there are two others variants, i.e., a spinner with text that where it is possible to write or withdraw an item from the spinner and a spinner with a writeable text where every choice is added 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 and Window; this last sets the background form with colour or gradients and a possibly transparency level and allows to position the form on the screen. The pseudo field After is useful for insert button or check box or radio buttons at right of a view (and also to the Title).

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‬)) or use the FontAwesome or Material Icons fonts for labels, title, and comment.

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

VBScript
...
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
	fg.fg(Activity,sample,Me,"handleAnswer","handleEvents")
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)
	Else
		Select parm(0)
			Case "Consent"
				fg.disable("BtnGo")
				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.

An 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.

Since formgen allows you to have multiple forms at the same time, it is possible to send back a form and subsequently recall it in the foreground.
This is achieved by specifying the hide parameter in the title of the form:

Title,History,Patient: " & name & ",,blue left,Hide;
Hide inserts at right of the Title that permits to send in background the form; when the form is in background appears at top right of the screen that allows to return the form to the front.

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.

VB.NET
...
& "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,,,,0 220;" _
& "S,Weight,,,,0 130;" _
& "S,Min,Minimum,,,20 200;" _
& "S,Max,Maximum,,,40 280;" _
& "S,HeartRate,Heart rate,,,0 250;" _
& "S,Temperature,,,36.5,30 48;" _
& "S,Saturation,,,,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?

History

  • 27th June, 2017: Initial version

License

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


Written By
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
Others
feb 2010 Burundi Kiremba hospital: MS Office course
feb 2011 Congo DR Kampene hospital: MS Office course

Comments and Discussions

 
QuestionWow Pin
Pam Kolep22-Oct-22 6:04
Pam Kolep22-Oct-22 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.