12 Example programs
12.2 Program with 3 Activities (ThreeActivityExample.b4a)
12.3.1 ScrollView example program
Let us make a ScrollView example with following functions:
Read a csv file and display it in a table based on a ScrollView.
The ScrollView can be scrolled vertically with the standard scrolling function of the ScrollView.
The ScrollView can also be scrolled horizontally with a Seekbar or dynamically with the finger on the lower blue rectangle ( SeekBar visible or not).
Clicking on a cell highlights the row and the cell, this routine allows adding other functions related to a row or a cell.
Clicking on a header, displays the column, this routine allows adding functions related to a column.
12.3 Scrollview example program 139 Basic4Android Beginner's Guide We define following variables and their default values:
StringUtils1 Used to read the csv file.
NumberOfColumns Number of columns.
RowHeight Height of a row in the ScrollView.
RowLineWidth Width of the lines between the rows.
RowHeight_1 Internal height of a row
RowHeight_1 = RowHeight - RowLineWidth ColLineWidth Width of the lines between the columns.
ColumnWidth() Width of the different columns as an array
ColumnWidth_1() Internal width of the different columns.
TotalColumnWidth() Coordinates of the left border of a column as an array.
HeaderColor Headers background color.
HeaderFontColor Headers font color.
HeaderLineColor Headers line color.
LineColor Cell line color.
CellColor Cell background line color.
FontColor Cell font line color.
Alignment Text alignment of the text in the headers and cells.
SelectedRow Index of the selected row.
SelectedRowColor Color of the selected row.
SelectedCellColor Color of the selected cell.
Type RowCol (Row As Int, Col As Int)
Define a custom variable that contains a row and a column index.
MoveLeft0 Used for the horizontal scrolling.
MoveX0 Used for the horizontal scrolling.
MoveX1 Used for the horizontal scrolling.
DeltaScroll Used for the horizontal scrolling.
DeltaX Used for the horizontal scrolling.
Time0 Used for the horizontal scrolling.
Personally, I prefer working with variables rather than with values. The maintenance and modification of a program is much easier with variables than with values.
12.3 Scrollview example program 140 Basic4Android Beginner's Guide
Now we define the Views for the program:
scvPersons ScrollView to display show the data.
pnlHeader Panel to display the headers.
skbScroll Seekbar to scroll the ScrollView and Header.
pnlScroll Panel for the 'dynamic' horizontal scrolling.
Timer1 Timer used the 'dynamic' horizontal scrolling.
12.3 Scrollview example program 141 Basic4Android Beginner's Guide Now we initialize the different Views and variables, we:
Initialize the panel for the horizontal scrolling.
Initialize the SeekBar for the horizontal scrolling.
Initialize the ScrollView
Initialize the internal column width and the left coordinates for each column and the total width of all columns.
Initialize the ScrollView width.
Set the Max parameter for the Seekbar
Set the index of the selected row to -1, no row selected.
Load the csv file, set the headers and fill the ScrollView.
Initialize the Timer for the horizontal scrolling.
12.3 Scrollview example program 142 Basic4Android Beginner's Guide Then we read the csv file, fill the headers and the table (ScrollView).
First, if the headers do exist, we read the csv file with the headers.
Or, if the headers do not exist, we read the csv file without the headers and set default header names Col1, Col2 etc.
Get the number of columns.
Display the headers SetHeader(h).
Display the table, by adding the different rows to the ScrollView AddRow(row).
12.3 Scrollview example program 143 Basic4Android Beginner's Guide To display the headers we:
Initialize the header panel.
Set the header panel color to the header line color.
Initialize a Label for each column name.
Set the different parameters for the labels.
Add the Labels onto the header panel.
Add the header panel to the Activity
12.3 Scrollview example program 144 Basic4Android Beginner's Guide Filling a row of the ScrollView with the AddRow routine:
First we check if the number of cells is equal to the number of columns.
Initialize a Label for each cell in the row.
Set the different parameters of the cell.
Initialize a RowCol variable, rc, for the label tag.
Set rc.Row to the row index and rc.Col to the column index.
Set the label tag to rc.
Add each label to the ScrollView.
Set the height of the internal panel of the ScrollView.
Note: an underscore character at the end of a line, like in line 212 means 'continue same instruction next line'.
12.3 Scrollview example program 145 Basic4Android Beginner's Guide Other functions:
Cell_Click
Click event of one of the cells in the table.
o Dim rc as a RowCol variable and Dim l as a Label o Set l equal to the Sender, the View that raised the event o Set rc equal to the Sender Tag parameter
o Call the SelectRow routine
o Display in the Activities title the row and column indexes and the cell content.
Header_Click
Click event of one of the header cells in the table.
o Dim l as a Label and Dim col as an integer o Set I equal to the Sender
o Set col equal to the Sender Tag parameter, which is the column index.
o Display the selected column in the Activity title.
12.3 Scrollview example program 146 Basic4Android Beginner's Guide
SelectRow
Routine managing the colors showing a selected row and cell.
It is called from the Cell_Click routine o Dim col as an integer.
o If there is a row selected, set the normal cell color.
o Set the SelectedRow variable to the new selected row index..
o Set the selected row and selected cell colors.
12.3 Scrollview example program 147 Basic4Android Beginner's Guide
GetView
Gets the Label object for the given row and column.
o Dim l as a Label.
o Gets the View in the given row and column, the view index in the ScrollView panel is equal to Row * NumberOfColumns + Col .
o Returns the Label.
GetCell
Gets the text of the Label for the given row and column.
o Gets the View in the given row and column.
o Return the Views Text parameter.
SetCell (not used in the program)
Sets the text of the Label for the given row and column.
o Gets the View in the given row and column o Sets the Views Text parameter to the given value
ClearAll
o Removes all Views (Labels) from the ScrollView Panel o Sets the ScrollView Panel Height to 0
o Sets the select row index to -1, no row selected
12.3 Scrollview example program 148 Basic4Android Beginner's Guide
Horizontal moving with the SeekBar
o Sets the Left parameter of the Header panel and the ScrollView.
o The SeekBar Max value was set to
skbScroll.Max = scvPersons.Width - Activity.Width.
Horizontal scrolling with the scroll panel.
o pnlScroll_Touch and Timer1_Tick.
o I leave it up to you to find how these work.
The basic principle it to calculate the speed between ACTION_DOWN and ACTION_UP and in the Timer routine to move dynamically the header and the Scrollview and reducing the speed.