Android Question Adding views to panel by end-user?

syncmaster13

Member
Licensed User
Longtime User
Hi

I’m trying to create a panel that will contain buttons added by user.
Before I go any farther I’d like to know if it is possible to allow end-user to name new created buttons and add a numerical value to it (which both will be displayed as a text and store in SQL database)?


Thank You

B4X:
Sub Add_Button_Click

Dim btn(10) As Button

number = number + 1
btn(number).Initialize("button")
'Msgbox("Button Added", "Success")

Panel1.AddView(btn(number), Button1.Left, toppos, Button1.Width, Button1.Height)

Panel1.Invalidate

btn(number).BringToFront
btn(number).Color = Colors.Green
btn(number).Visible = True
btn(number).Text = (" Custom  :  " & (number))
btn(number).TextSize = 25dip
btn(number).Enabled = True
btn(number).TextColor = Colors.Blue
btn(number).Tag = number
Panel1.Height = toppos + 13%x
Panel1.Invalidate
ScrollView1.Panel.Height = Panel1.Height


toppos = toppos + 11%x
End Sub

Sub button_click
Dim b As Button
    b = Sender
Msgbox("Button Clicked" , b.tag)
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
You did it!

You put a text in the new button:
btn(number).Text = ("Custom" & (number)) - (you can ask it to the user via a dialog or a EditText)

and you put the number in the tag.

You have used an event.

Instead of using the array Dim btn(10) As Button,
use a List but put it in the Globals.

Remember that you can put an object in the tag.
You can create a type, then a variable of that type and put it in the tag

Type typButCode(Name as string, Code as int)

dim lstButtons as list : lstButtons.Initialize

Dim ButCode as typButCode
Dim NewButton as Button
NewButton.initialize ("NewButton")
NewButton.Tag = ButCode
lstButtons.Add(NewButton)
 
Last edited:
Upvote 0

syncmaster13

Member
Licensed User
Longtime User
Thank You

It was very helpful. I didn't have a lot of time to spend on the code and still have to implement "Tag" for buttons. but for now as a end-user, I'm able to create new buttons and add description + numbers. For now the code looks like this:
B4X:
Sub Process_Globals

End Sub

Sub Globals
Dim number As Int = 0
Dim toppos As Int = 13%x

    Dim Button1 As Button
    Dim ScrollView1 As ScrollView
    Dim Panel1 As Panel

    Dim lstButtons As List
    Type typButCode(Name As String, Code As Int)
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    Activity.AddMenuItem("Add New ","Add_Button")
   
ScrollView1.Panel.LoadLayout("pnl_for_layout1")
ScrollView1.Panel.Height = Panel1.Height
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Add_Button_Click

    Dim Id As InputDialog
    Id.InputType = Id.INPUT_TYPE_TEXT
    Id.Input = ""
    Id.Hint = "Enter Your Button Name !"
    Id.HintColor = Colors.ARGB(196, 255, 140, 0)
    ret = DialogResponse.CANCEL
    ret = Id.Show("Input the required text", "SPEED", "Yes", "No", "", Null)
    'ToastMessageShow(ret & " : " & Id.Input, False)   

If Id.Response = -1 Then  ' answer yes
  Dim nd As NumberDialog
    nd.Digits = 7
    nd.Number = 0000000
    nd.Decimal = 2
    nd.ShowSign = False
    ret = nd.Show("SET SPEED ", "Yes", "No", "", Null)   
    'ToastMessageShow(ret & " : " & nd.Number, False)
     
            If nd.Response = -1 Then
          lstButtons.Initialize

                  Dim ButCode As typButCode
                    Dim NewButton As Button
                    NewButton.initialize ("NewButton")
                    NewButton.Tag = ButCode
                    lstButtons.Add(NewButton)

                    Panel1.AddView(NewButton, Button1.Left, toppos, Button1.Width, Button1.Height)
                    Panel1.Invalidate

                    NewButton.Color = Colors.Blue
                    NewButton.Text = (Id.Input & "  :  " & nd.Number)

                    NewButton.initialize ("NewButton")
                    NewButton.TextSize = 25dip
                    NewButton.Enabled = True
                    NewButton.TextColor = Colors.Black
                    'NewButton.Tag = 

                    Panel1.Height = toppos + 13%x
                    ScrollView1.Panel.Height = Panel1.Height                   
                    toppos = toppos + 11%x
            Else
               ret = DialogResponse.CANCEL
            End If     

Else
      ret = DialogResponse.CANCEL
End If       

End Sub

Sub NewButton_click
Dim b As Button
    b = Sender
Msgbox("Button Clicked" , b.tag)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…