Share My Creation MuEdit - multiple EditText in a custom dialog

For a VB6 project of mine, I used a universal form to show and edit a set of entries (of a database or just some settings).
Translating this to B4A, I created a little helper which might be useful to other users. It is not perfect, but I started with B4A two weeks ago...

Call it just as
B4X:
ret = MuEdit(Title As String, Labels As List, Values As List, Hints As List, Labelwidth As Int)
It will return ret=-1 if the OK button was pressed, and the Values list updated with the new entries, otherwise is ret=-2 and Values remain unchanged.
Labelwidth (from 10 to 90, like %) is used to balance the label width with the EditText. 50 means equal width for both.

I use CustomDialog (from DIALOG library) to create a panel with a number of labels and EditTexts according to the size of the Label list.
This dialog is "modal" (as much as it can be - it will be destroyed by rotating the device). No global variables or views or event handling SUBs are necessary - just continue inside the SUB which calls MuEdit.

This is the code. I appreciate every improvement, especially of naming the buttons automatically according to the language used.

B4X:
Sub MuEdit(Title As String, Labels As List, Values As List, Hints As List, Labelwidth As Int) As Int
'   Multiple line input dialog. Uses Lib 'Dialogs' by Andrew Graham
'   Dimension of Lists has to be equal or at least that of 'Labels'
'   This SUB is modal. No global variables or additional SUBs are necessary. It will return
'   the original ret value of CustomDialog. Values are changed only if "yes"/"OK" button is pressed.
'   Please adapt the Button Texts of CustomDialog as you need them
'   Use Labelwidth to vary the relation between prompts/labels and EditTexts. 10-90. 50 means equal.

   Dim cd As CustomDialog
   Dim scv As ScrollView
   Dim NumberofItems,i As Int
   Dim lwidth,ewidth As Int
   NumberOfItems = Labels.Size
   If Labelwidth<10 Then Labelwidth = 10
   If Labelwidth>90 Then Labelwidth = 90
   scv.Initialize((NumberofItems+2)*50dip)
   lwidth = (90%x*Labelwidth/100-6)*1dip
   ewidth = (90%x*(1-Labelwidth/100)-10)*1dip
   scv.Color = Colors.DarkGray
   Dim InLabel(NumberOfItems) As Label
   For i=0 To NumberOfItems-1
      Inlabel(i).Initialize(""): Inlabel(i).TextSize = 14
      InLabel(i).Gravity=Gravity.CENTER_VERTICAL
      Inlabel(i).Text = Labels.Get(i)
      scv.Panel.AddView(InLabel(i),5dip,i*50dip,lwidth,46dip)
   Next
   Dim MuEditInputs(NumberOfItems) As EditText 
   For i=0 To NumberOfItems-1
      MuEditInputs(i).Initialize("")
      MuEditInputs(i).Text = Values.Get(i)
      MuEditInputs(i).Hint = Hints.Get(i): MuEditInputs(i).HintColor = Colors.LightGray
      scv.Panel.AddView(MuEditInputs(i),lwidth+10dip,i*50dip+2dip,ewidth,50dip)
   Next
   i=i*50dip: If i > 90%y Then i=90%y
   cd.AddView(scv, 0,0,90%x,i) ' sizing relative to the screen size is probably best
'adapt your button texts as needed here. I do not use the "no" button.
   ret = cd.Show(Title, "OK", "", "Cancel", Null)      
   If ret = -1 Then    '
      For i=0 To MuEditInputs.Length-1
         Values.Set(i, MuEditInputs(i).Text)
      Next
   End If
   Return ret
   
End Sub
 

Pfriemler

Member
Licensed User
Longtime User
A screenshot, labelwidth = 40. Sorry for the German title...
 

Attachments

  • MuEditPicPfriemler.jpg
    26.9 KB · Views: 1,177

demasi

Active Member
Licensed User
Longtime User
Very Good!!

thanks for sharing this with us. Fantastic job you did. it will be very usefull. congratz!:sign0060:
 

Eduard

Active Member
Licensed User
Longtime User
Thanks,

I use it with some minor modifications.
B4X:
Private Sub Input_DB(title As String, fieldnames As List, fieldvalues As List, hints As List, labelwidth As Int) As Int
'    Multiple line input dialog. Uses Lib 'Dialogs' by Andrew Graham
'    Dimension of Lists has to be equal or at least that of 'Labels'
'    This SUB is modal. No global variables or additional SUBs are necessary. It will return
'    the original ret value of CustomDialog. Values are changed only if "yes"/"OK" button is pressed.
'    Please adapt the Button Texts of CustomDialog as you need them
'    Use Labelwidth to vary the relation between prompts/labels and EditTexts. 10-90. 50 means equal.

   'It will Return DialogResponse.POSITIVE If the OK Button was pressed, AND the Values List updated with the new entries, otherwise remain unchanged.
   'Labelwidth (from 0 To 90, like %) Is used To balance the Label width with the EditText. 50 means equal width For both.

   Dim returnw As Int
    Dim cd As CustomDialog
    Dim scv As ScrollView
    Dim NumberofItems,i As Int
    Dim lwidth,ewidth As Int
    NumberofItems = fieldnames.Size
    labelwidth=Min(90,Max(labelwidth,0)) 'labelwidth can be 0 
    scv.Initialize((NumberofItems+0)*50dip)
    lwidth = (90%x*labelwidth/100-6)*1dip
    ewidth = (90%x*(1-labelwidth/100)-10)*1dip
    scv.Color = Colors.DarkGray
    Dim InLabel(NumberofItems) As Label
    For i=0 To NumberofItems-1
        InLabel(i).Initialize(""): InLabel(i).TextSize = 14
        InLabel(i).Gravity=Gravity.CENTER_VERTICAL
        InLabel(i).Text = fieldnames.Get(i)
        scv.Panel.AddView(InLabel(i),5dip,i*50dip,lwidth,48dip)
    Next
    Dim MuEditInputs(NumberofItems) As EditText 
    For i=0 To NumberofItems-1
        MuEditInputs(i).Initialize("")
      MuEditInputs(i).SingleLine=True 'single line
        MuEditInputs(i).Text = fieldvalues.Get(i)
        MuEditInputs(i).Hint = hints.Get(i): MuEditInputs(i).HintColor = Colors.LightGray
        scv.Panel.AddView(MuEditInputs(i),lwidth+10dip,i*50dip+2dip,ewidth,50dip)
    Next
    cd.AddView(scv,0,0,90%x,Min(90%y-100dip,i*50dip)) ' sizing relative to the screen size is probably best -100dip
    returnw = cd.Show(title,"OK","Cancel","",Icon)        
    If returnw = DialogResponse.POSITIVE Then     '
        For i=0 To MuEditInputs.Length-1
            fieldvalues.Set(i, MuEditInputs(i).Text)
        Next
    End If
    Return returnw
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…