Share My Creation ComboBox by code

Hello to all.
This time is an experiment rather than a program that i present you
If RemMe (that you can find here in other 3D) was my first program under Android it was also the translation of one Basic program under Windows.
One of the objects of the Windows version that i had to replace in the Android/B4A layout is the ComboBox.
I know that B4a uses Spinner that remembers a ComboBox but i dont like it much, even if it is surely much more powerful and integrated of my experimental combo.
So i am trying to create my combobox using the code (as simple as possible to use it in other programs if necessary).
Basically the problem is to create items indexed using something that do not has this property.

The behaviors required to Android ComboBox are as follows:
- Writing of text elements in Inputbox and loading the text on the list by pushing the button on the side.
- List of 10 elements (maximum for current experiment)
- Selection of a list element with simple touch with transfer of selection in the inputbox
- Clear of the InputBox with the use of the arrow key and call of the entire list with a subsequent use of the same key.

To achieve this, I used a EditBox, an ImageView and a Label placed as seen from the attached image to form a ComboBox (actually the label is initially empty and invisible.
Since they are 3 customizable standard views by the designer, combo box can be customized (background,color etc) as user likes

First, the EditBox enables the keyboard and once wrote the text in it using 'ImageView as a button (Click event) the transfer is made of what is written in the list (label) that appears below the EditBox.
Continuing to write new texts in inputbox the label extends automatically to accommodate the new elements of the list.
It should be noted that, at the same time an item is added to the list, the same text is also added to an array of strings (associating them so an index).

Now it comes the worse. Select in the Label/List a single element is not difficult but it becomes so if we want to pull it out with a single touch.
To do this I add a panel (kept transparent) and I use his property to return the coordinates of the touch position. (just the Y)

One thing that I did not say is that the label (list) each time you add an item stretches a strip of fixed dimensone (16 for label height, 12-point font Bold) and the Panel does exactly the same thing.
This means that if the Y coordinate falls into a precise interval 0-16/17-32/33-48 etc you can assign it an index using the Select Case statement.
Obtained the index (float value is converted into Int) just call the element, with the index found, from the array and insert it as the text of the EditBox and you're done.
This below is the code:

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim ComboTxt As EditText
Dim Img1 As ImageView
Dim L1 As Label
Dim Li(11)
Dim n, D, t As Int
Dim Panel1 As Panel
Dim KY As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Activity.LoadLayout("Combo0")
n = 0
D = L1.Height

End Sub

Sub L1_Click 'Extracts the index by the coordinates of the touch screen
Select Case True
Case KY < (D*2) : t = 1 : ComboTxt.Text = Li(t)
Case KY < (D*3) : t = 2 : ComboTxt.Text = Li(t)
Case KY < (D*4) : t = 3 : ComboTxt.Text = Li(t)
Case KY < (D*5) : t = 4 : ComboTxt.Text = Li(t)
Case KY < (D*6) : t = 5 : ComboTxt.Text = Li(t)
Case KY < (D*7) : t = 6 : ComboTxt.Text = Li(t)
Case KY < (D*8) : t = 7 : ComboTxt.Text = Li(t)
Case KY < (D*9) : t = 8 : ComboTxt.Text = Li(t)
Case KY < (D*10) : t = 9 : ComboTxt.Text = Li(t)
Case KY < (D*11) : t = 10 : ComboTxt.Text = Li(t)
End Select
L1.Visible = False
End Sub

Sub Img1_Click 'It handles the behavior of CombBox
If ComboTxt.Text = "" AND L1.Text = "" Then
Return
Else
If ComboTxt.Text = "" AND L1.Text <> "" AND L1.visible = False Then
L1.visible = True
Else
L1.Visible = False
End If
End If
If ComboTxt.Text <> "" Then
For w = 1 To n
If ComboTxt.Text = Li(w) Then ComboTxt.Text = ""
Next
End If
If ComboTxt.Text <> "" AND n < 11 Then
n = n + 1
L1.Text = L1.Text & CRLF & ComboTxt.Text
Li(n) = ComboTxt.Text
L1.Height = L1.Height + D : Panel1.Height = L1.Height
L1.Visible = True
ComboTxt.Text = ""
Panel1.RequestFocus
End If
End Sub

Sub Panel1_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Read coordinate
If Action = Activity.ACTION_DOWN Then
KY = Round(Y)
End If
L1_Click
End Sub

As experiment I do not even classify as Beta. Obvious that the project need still to be optimized.
For the moment I still have to add the deletion of a single element and of the entire list while preloading of a whole list is under test .
If you use the ComboBox leaf as a simple menu of course you will have to enter in the code of the application or set the desired elements reading them from file at startup.
And i know also i have to increase distance of the items of the list to enable a better use by touch of the screen even if on my old GT P100 Samsung the selection works well.
The program works correctly on emulator 320x480 160dpi (base 1)
Have fun. (More detaill will follow)
 

Attachments

  • Combo3.png
    Combo3.png
    11.3 KB · Views: 2,369
  • Combo1.apk
    106.1 KB · Views: 355
  • Combo1.png
    Combo1.png
    26.9 KB · Views: 430
  • Combo2.png
    Combo2.png
    12.8 KB · Views: 447
Last edited:

efsoft

Member
Licensed User
Longtime User
Hello to all,
this is just n example (destined to become a real application on my program Remme) of the experimental ComboBox used as the main element of the menu bar command.
When compared with the bar currently used in the program (found it here in its 3D) can be seen as provide easier use, the same functions managed, and a simplification of the encoding of the program (at least in my hypothesis).
Annex apk file contains an example of its operation in a first simulation.
The images show how the Combo Bar works now.
Thanks for attention. Have fun.
 

Attachments

  • Combo1.apk
    108.3 KB · Views: 341
  • ComboRemMe1.png
    ComboRemMe1.png
    20.3 KB · Views: 415
  • ComboRemMe2.png
    ComboRemMe2.png
    18.6 KB · Views: 396
  • ComboRemMe3.png
    ComboRemMe3.png
    19.6 KB · Views: 411
Last edited:

efsoft

Member
Licensed User
Longtime User
Hello to all,
an addition to the code, omitted for distraction and an upgrade of the code (it was already in the file. apk posted before but not reported as a code).

The first thing is the behavior of the panel. As said the panel should be transparent but, if not checked his presence , happens ,when the list made invisible (as the panel), that one touch on the screen shows the entry, accidentally touched even if invisible, in the inputbox of the Combo.
The following changes to the previous code fix the problem.

Sub L1_Click 'Coordinate/index converter
Select Case True
Case KY < (D*2) : t = 1 : ComboTxt.Text = Li(t)
Case KY < (D*3) : t = 2 : ComboTxt.Text = Li(t)
Case KY < (D*4) : t = 3 : ComboTxt.Text = Li(t)
Case KY < (D*5) : t = 4 : ComboTxt.Text = Li(t)
Case KY < (D*6) : t = 5 : ComboTxt.Text = Li(t)
Case KY < (D*7) : t = 6 : ComboTxt.Text = Li(t)
Case KY < (D*8) : t = 7 : ComboTxt.Text = Li(t)
Case KY < (D*9) : t = 8 : ComboTxt.Text = Li(t)
Case KY < (D*10) : t = 9 : ComboTxt.Text = Li(t)
Case KY < (D*11) : t = 10 : ComboTxt.Text = Li(t)
End Select
L1.Visible = False : Panel1.Enabled = False
End Sub

Sub Img1_Click 'Combo Behaviour Module base
If ComboTxt.Text = "" AND L1.Text = "" Then
Return
Else
If ComboTxt.Text = "" AND L1.Text <> "" AND L1.visible = False Then
L1.visible = True : Panel1.Enabled = True
Else
L1.Visible = False : Panel1.Enabled = False
End If
End If
If ComboTxt.Text <> "" Then
For w = 1 To n
If ComboTxt.Text = Li(w) Then ComboTxt.Text = ""
Next
End If
If ComboTxt.Text <> "" AND n < 11 Then
n = n + 1
L1.Text = L1.Text & CRLF & ComboTxt.Text
Li(n) = ComboTxt.Text
L1.Height = L1.Height + D : Panel1.Height = L1.Height
L1.Visible = True
ComboTxt.Text = ""
Panel1.Enabled = True
Panel1.RequestFocus
End If
End Sub

the second update is useful to load in a single operation multiple items in a list (in the case of use of the combo as menu for example)
This is the code.

Sub LoadList
Dim Lista, Nlist as String 'Rapid Add items module
Dim Tx as integer
Lista = "Add new record,Modify record,Delete record,"
Do Until Lista.length = 0
Tx = (Lista).IndexOf(",")
ComboTxt.Text = Lista.SubString2(0, Tx)
Img1_Click
Nlist = Lista.SubString2(Tx+1, Lista.Length) '(remaining Lista string)
Lista = Nlist
Loop
End Sub

You will notice that the code does not fill the Label L1 (List) in the usual manner because, to be functional, it has to load each item individually as for the coding rules of the simulated ComboBox.
The code looks like a common section - Sub / End Sub that can be added or removed without problems in the program if it serves or not a similar function.
Have fun. To the next.
 
Last edited:

efsoft

Member
Licensed User
Longtime User
We go to practice

Hello to all,
As I wrote, I'm using the experimental ComboBox instead of Android ListView to create pop-up menus for an update of my program RemMe.
In doing this I had necessarily to revise the code previously posted since the use as a menu is definitely simpler than the ComboBox and has different needs.

The first thing: there is no need of the button of combobox that normally opens and closes the list box to store in it the elements passed as input.
All the items you need will be loaded by the program at start up and the entries are already defined and, if they are for a menu of 4 elements. is useless manage 10 items as in the original example .

As menu does not need also to input nothing, this allows you to change the structure of the ComboBox using 2 labels only, instead of EditBox, Label and ImageView. The only element indispensabie is the Panel which must be calibrated to better select, in small screens, the distance of the items to be selected at the touch.

So the final code (almost) to apply to RemMe, but to any other menu also, becomes the following:
(I tried to add us notes for each piece of code.)

Sub Globals
Dim Li(4)
Dim Panel1 As Panel
Dim L1 As Label
Dim Id, D, n As Int
Dim KY As Float
Dim Lista, Lr, La, Nlist As String
Dim Tx As Int
........

End Sub

Sub Activity_Create(FirstTime As Boolean)
..........
n = 0
D = L1.Height '(i.e 16)
End Sub

Sub Lf1_Click 'Coordinate/index converter.
Select Case True You can see that the items reduced to 4 only)
Case KY < (D*2) : n = 1 : Lfun.Text = Li(n)
Case KY < (D*3) : n = 2 : Lfun.Text = Li(n)
Case KY < (D*4) : n = 3 : Lfun.Text = Li(n)
Case KY < (D*5) : n = 4 : Lfun.Text = Li(n)
End Select
L1.Visible = False : L9.Visible = False 'L9 is the frame of the label L1 (the list)
Panel1.Enabled = False
Lmen 'this is a sub to check if action selected can executed correctly
End Sub

Sub GesMen 'Combo Behaviour Module base
The RED code is the one deleted
being useless for the menu use

'If Lfun.Text = "" AND L1.Text = "" Then
'Return
'Else
'If Lfun.Text = "" AND L1.Text <> "" AND L1.visible = False Then
'L1.visible = True : L9.Visible = True
'Panel1.Enabled = True
'Else
'L1.Visible = False : L9.Visible = False
'Panel1.Enabled = False
'End If
'End If
'If Lfun.Text <> "" Then
'For w = 1 To n
'If Lfun.Text = Li(w) Then Lfun.Text = ""
'Next
'End If

This is instead the code to load
a single item into a line of the list

If Lfun.Text <> "" AND t < 4 Then
L1.Text = L1.Text & CRLF & Lfun.Text : n = n + 1
Li(n) = Lfun.Text
L1.Height = L1.Height + D : Panel1.Height = L1.Height
L1.Visible = True : L9.Visible = True 'L9 is only the frame of the L1 Label (the list)
Lfun.Text = ""
Panel1.Enabled = True
Panel1.RequestFocus
End If
End Sub

Sub Panel1_Touch (Action As Int, O As Float, V As Float) As Boolean
If Action = Activity.ACTION_DOWN Then
KY = Round(V)
End If
L1_Click The value will be used by the Sub L1 to obtain the index of the item.
End Sub

A peculiarity of the menu Remme (already available in previous versions) is to use a single list retractable driven by two different keys alternately.
This therefore requires that the exchange program to request the list of items displayed to the user.
This is the task the following code that differrisce from that used for the ComboBox itself.
Not being used the combobox button will be the keys that call to pass the instruction to make visible or invisible after updating the list in accordance with the given key.

Sub LFunList 'Rapid Add items module
L1.Text = ""
L1.Height = D
Panel1.Height = L1.Height
Panel1.Enabled = True
n= 0 : Dim Li(4)
If Archem.Enabled = False Then Lista = "Nuovo Record,Modif.Record,Canc Record,"
If Recorm.Enabled = False Then Lista = "Rinomina Arch.,Titoli Campi,Cambia Settaggi,"
Do Until Lista.length = 0
Tx = (Lista).IndexOf(",")
Lfun.Text = Lista.SubString2(0, Tx)
GesMen
Nlist = Lista.SubString2(Tx+1, Lista.Length) '(resto stringa da Sinistra)
Lista = Nlist
Loop
End Sub

Code has been tested under GB 2.3.3, ICS 4.0.4 and now also under JelyBeam 4.2 and appears to work correctly
Tanks for attention.
 

Attachments

  • ComboRem0.jpg
    ComboRem0.jpg
    29.8 KB · Views: 421
  • ComboRem1.jpg
    ComboRem1.jpg
    31 KB · Views: 421
  • ComboRem2.jpg
    ComboRem2.jpg
    31.6 KB · Views: 415
Last edited:

efsoft

Member
Licensed User
Longtime User
Hello to all
it may be that someone saw that my version of Remme, which uses the reduced code to use as menu is available in the forum.
Do not forget, however, that there is an additional feature that is still missing in the experimental ComboBox project.
Currently the code allows you to:

- Put items in the input box.
- Add each item added to the drop down list by pressing the arrow at side of the input box
- Avoid the inclusion of possible duplicates.
- To select, with a touch, the chosen element in the list, making it appear in the inputbox.
- To quickly load a list of items presented as a simple string where the elements are separated by a comma.

What is missing?
- To delete a single item from the list, or delete an entire list with a single tap.
This is what the code, found below, can do.

Sub Img1_LongClick 'Cancel Items module
Dim L2, Nlist2 As String
Dim Result, s, Tx As Int

If L1.Text <> "" Then
Result = Msgbox2("Confirm?","Delete list","Selected Item","Undo","Full List",Null)
If Result = DialogResponse.Positive Then
If ComboTxt.TextSize <> 0 Then
Do Until Li(t+1) = ""
Li(t) = Li(t+1)
t = t + 1
Loop
Li(t) = ""
For s = 1 To n-1
L2 = L2 & Li(s) & ","
Next
L1.Text = ""
L1.Height = D
ComboTxt.Text = ""
n=0
Do Until L2.length = 0
Tx = (L2).IndexOf(",")
ComboTxt.Text = L2.SubString2(0, Tx)
L1.Text = L1.Text & CRLF & ComboTxt.Text : n = n + 1
L1.Height = L1.Height + D : Panel1.Height = L1.Height
Li(n) = ComboTxt.Text : ComboTxt.Text = ""
Panel1.RequestFocus
Nlist2 = L2.SubString2(Tx+1, L2.Length)
L2 = Nlist2
Loop
End If
End If
If Result = DialogResponse.Cancel Then Return
If Result = DialogResponse.Negative Then
L1.Text = ""
Dim Li(11) : n = 0
L1.Height = D
L1.Visible = False
End If
End If
End Sub

This code is not fully optimized.
In fact it seems to work correctly if you add or delete element inserted in the list but sometime, if you load the list using the quick loading and
and you make subsequent additions to the list you may meet some problem at the time of the item selection (probably for synchronous errors.
Basically this code will be subject to possible modifications to solve the problem.
Stay tuned, in the meantime thanks for your attention and enjoy.
 
Last edited:

efsoft

Member
Licensed User
Longtime User
Found it!
Discovered the reason for the malfunction of the Combo Box in certain conditions.
The thing is due to my distraction. As. the simulation was working so I did not think to differentiate the variables used for writing and reading each item.
For this reason the value of the variable obtained in reading phase altered sometimes the correct value when you added an element out of sequence.
I changed the code appropriately posted here so far.
However, as soon as possible i shall add a file in zip format with 3 possible applications of the combo box code emulated.
Thanks for your attention and sorry for the oversight.
 

efsoft

Member
Licensed User
Longtime User
As promised, I add 2 file containing some programs that show the emulation of the ComboBox and its use.
The Zip files contains, a Folder that can be used in a B4A being the same that I used to develop the code.

In the first zip in thefolder (ComboAnd5) you will find the complete code of the ComboBox including Sub fast load elements and the Sub for the cancellation of the same (All or single element).
In the attached program the quick addition is controlled by the button "Add" (to use the Sub can also be called by the code itself if desired) while the cancellation is activated by a 'Long Click' on the arrow of the ComboBox.
If you use the code in a program you must eliminate the lines which control the Label1 that shows up the parameters to be monitored to check the synchronism in the development phase.

In the second zip in the folder you will find the use of the ComboBox emulated for the realization of the Top Bar of my program Remme. (already posted in the relevant thread)
For use as a menu you will use a small part of the code only because, as it is easy to understand, it is not necessary to use some of the functions of the Combo complete (the deletion of a single element for example).
Particular feature of this type of application is that the list manages alternately two different menu.

A third use of the ComboBox code mixed with the dynamic management of views (on the basis of the example provided by Tic.Tac.Toe B4A) to create lists retractable multiple elements is still in the development phase and when work as add will be subject to further update .
 

Attachments

  • ComboTrail.png
    ComboTrail.png
    13.1 KB · Views: 473
  • ComboEmulation.zip
    338 KB · Views: 469
  • ComboRemMe.zip
    340.5 KB · Views: 415
  • ComboRem0.jpg
    ComboRem0.jpg
    29.8 KB · Views: 425
Top