Android Question Problems with Button click event

Discussion in 'Android Questions' started by slavko, Feb 17, 2015.

  1. slavko

    slavko Member Licensed User

    Hi,

    in order to avoid use of soft nmeric keyboard, i created and initialized in activity 10 buttons. They are in a form of 3x3 matrix with button for zero in bottom. Problem, that i have is, that only first line of buttons is responsive on click. When i changed position of them, again only first line is responding. In sample code below i changed on-screen position of buttons, in this case button_click event is fireing with b4-b6 buttons, because they are positioned higher than button b1-b3.

    Did someone have a similar problems? Any suggestion, what to do?


    Code for initialization of Buttons:
    b1.Initialize("b1")
    Activity.AddView(b1, 68%x, 20%y - 60dip, 10%x, 50dip)
    b1.text = "1"
    b2.Initialize("b2")
    Activity.AddView(b2, 78%x, 20%y - 60dip, 10%x, 50dip)
    b2.text = "2"
    b3.Initialize("b3")
    Activity.AddView(b3, 88%x, 20%y - 60dip, 10%x, 50dip)
    b3.text = "3"
    b4.Initialize("b4")
    Activity.AddView(b4, 800dip, 10%y - 60dip, 10%x, 50dip)
    b4.text = "4"
    b5.Initialize("b5")
    Activity.AddView(b5, 78%x, 10%y - 60dip, 10%x, 50dip)
    b5.text = "5"
    b6.Initialize("b6")
    Activity.AddView(b6, 88%x, 10%y - 60dip, 10%x, 50dip)
    b6.text = "6"
    b7.Initialize("b7")
    Activity.AddView(b7, 68%x, 30%y - 60dip, 10%x, 50dip)
    b7.text = "7"
     
  2. klaus

    klaus Expert Licensed User

    You must post more code, or better a small project showing the problem.
    The problem is not in the code you poseted.

    You could also use the same Click event routine for all buttons using the Tag property.
    There is an exapmle in the Beginne's Guide, chapter 3 Second program (SecondProgram.b4a).
     
    Rob Rendle likes this.
  3. Cableguy

    Cableguy Expert Licensed User

    The first thing that strikes me is why are you mixing % values with dip like the 800dip value in one of your lines...
    Still this is not your problem...
    are all button click events created? Are you sure you didn't double any of them?

    You should consider using one single event for all your buttons and inside it trap the one clicked, like

    Code:
    sub mybuttons_click
    Dim V as button
    V = 
    sender
    log ($"you just clicked ${V.Text} button"$)
    End sub
    [Edit] @klaus posted while I was typing... dahm phone! LoL
     
    DonManfred likes this.
  4. eurojam

    eurojam Well-Known Member Licensed User

    and you can light your code extremly, if you are using an array of buttons for making a grid of buttons - that is much more easier then define each button. see example below with the part of cableguy in it
    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 bt(8As Button
    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")
        Dim delta As Int = 10dip
        
    Dim h As Int = 50dip
      
        
    For i=0 To bt.Length-1
          bt(i).Initialize(
    "mybuttons")
          bt(i).Color = 
    Colors.Red
          bt(i).Text=i
          
    Activity.AddView(bt(i),20dip,0 + i*(delta+h), 50dip, h)
        
    Next

    End Sub

    Sub mybuttons_click
    Dim V As Button
    V = 
    Sender
    Log ($"you just clicked ${V.Text} button"$)
    End Sub
     
    Cableguy likes this.
  5. slavko

    slavko Member Licensed User

    Hi,
    reason for mixing is, that i was testing different ways of positioning of buttons in order to get click event.

    below is code for activity_create and click events for buttons. in this case buttons b1,b2, b3, btnDB and btnCVS are responsive for clicking, others not.
    i can add whole project, if needed, it is still in development...

    Sub Activity_Create(FirstTime AsBoolean)
    If FirstTime Then
    ' File.Delete(File.DirDefaultExternal, DBFileName) ' for testing to remove an old file
    IfFile.Exists(File.DirDefaultExternal, DBFileName) = FalseThen
    File.Copy(File.DirAssets, DBFileName, DBFileDir, DBFileName)
    EndIf
    SQL1.Initialize(DBFileDir, DBFileName, True)
    EndIf

    b1.Initialize("b1")
    Activity.AddView(b1, 68%x, 10%y -60dip, 10%x, 50dip)
    b1.text = "1"
    b2.Initialize("b2")
    Activity.AddView(b2, 78%x, 10%y -60dip, 10%x, 50dip)
    b2.text = "2"
    b3.Initialize("b3")
    Activity.AddView(b3, 88%x, 10%y -60dip, 10%x, 50dip)
    b3.text = "3"
    b4.Initialize("b4")
    Activity.AddView(b4, 68%x, 20%y - 60dip, 10%x, 50dip)
    b4.text = "4"
    b5.Initialize("b5")
    Activity.AddView(b5, 78%x, 20%y - 60dip, 10%x, 50dip)
    b5.text = "5"
    b6.Initialize("b6")
    Activity.AddView(b6, 88%x, 20%y - 60dip, 10%x, 50dip)
    b6.text = "6"
    b7.Initialize("b7")
    Activity.AddView(b7, 68%x, 30%y - 60dip, 10%x, 50dip)
    b7.text = "7"
    b8.Initialize("b8")
    Activity.AddView(b8, 78%x, 30%y - 60dip, 10%x, 50dip)
    b8.text = "8"
    b9.Initialize("b9")
    Activity.AddView(b9, 88%x, 30%y - 60dip, 10%x, 50dip)
    b9.text = "9"
    bo.Initialize("bo")
    Activity.AddView(bo, 78%x, 40%y - 60dip, 10%x, 50dip)
    bo.text = "0"
    Potrdi.Initialize("Potrdi")
    Activity.AddView(Potrdi, 78%x, 60%y - 60dip, 10%x, 50dip)
    Potrdi.text = "Potrdi"

    btnDB.Initialize("btnDB")
    Activity.AddView(btnDB, 34%x, 100%y - 60dip, 30%x, 50dip)
    btnDB.text = "SwapDB"
    textbox.Initialize("btnDB")
    Activity.AddView(textbox, 2%x, 100%y - 60dip, 30%x, 50dip)
    textbox.text = "stevilo"
    btnCVS.Initialize("btnCVS")
    Activity.AddView(btnCVS, 68%x, 100%y - 60dip, 30%x, 50dip)
    btnCVS.text = "CVS"
    pnlTable.Initialize("")
    Activity.AddView(pnlTable, 20dip, 20dip, 100%x - 40dip, 100%y - 90dip)
    Table1.Initialize(Me, "Table1", 5, Gravity.CENTER_HORIZONTAL, True)
    Table1.CellAlignment = Bit.OR(Gravity.LEFT, Gravity.CENTER_VERTICAL)
    Table1.HeaderColor = Colors.Blue
    Table1.HeaderTextColor = Colors.Yellow
    Table1.TextColor = Colors.Blue
    Table1.TableColor = Colors.Red
    Table1.RowHeight = 50dip
    Table1.AllowSelection = False
    Table1.AddToActivity(pnlTable, 0, 0, pnlTable.Width, pnlTable.Height)

    ' Table1.MultiSelect = True
    Table1.MultiSelect = False
    Table1.SortColumn = False
    Table1.SingleLine = False
    FillTableCSV
    End Sub

    clik events:
    Sub bo_Click()
    textbox.Text= textbox.text.trim & "0"
    End Sub
    Sub b1_Click()
    textbox.Text= textbox.text.Trim & "1"
    End Sub
    Sub b2_Click()
    textbox.Text= textbox.text.trim & "2"
    End Sub
    Sub b3_Click()
    textbox.Text= textbox.text.trim & "3"
    End Sub
    Sub b4_Click()
    textbox.Text= textbox.text.trim & "4"
    End Sub
    Sub b5_Click
    textbox.Text= textbox.text.trim & "5"
    End Sub
     
  6. Cableguy

    Cableguy Expert Licensed User

    Please use the "Code" tags in order to make your code readable
     
    eurojam likes this.
  7. eurojam

    eurojam Well-Known Member Licensed User

    Slavko,
    you can use the
    Code:
    ...your code...
    tags, that's make it much more easier to read your post with code in it....
     
    Cableguy likes this.
  8. klaus

    klaus Expert Licensed User

    Isn't the pnlTable view covering buttons ?
    Please post the project (or a smaller one) as a zip file, IDE File / Export As Zip so we could test it.
     
  9. Cableguy

    Cableguy Expert Licensed User

    Is it me or you have not declared your buttons in Globals???

    Code:
    Dim Bo, b1,b2,b3 as Button
    Every view must first be declared before initialized
     
  10. slavko

    slavko Member Licensed User

    Sorry, i am a new person on this forum, i was not aware,that i can add in form of tags, now i see...

    I tried code with button as array, but again same problem: only button with number 1, that is the top first button is trigerring click event. I am working with SAMSUNG TAB 3.


    This time i add a code that it is causing a probem...
     

    Attached Files:

  11. DonManfred

    DonManfred Expert Licensed User

    +100 !!
     
  12. eurojam

    eurojam Well-Known Member Licensed User

    slavko,
    it seems that the events are all ok. may be you can't see it in your textbox, because with every event, you are adding the number of your button to the end of your result string. Try to use
    Code:
    textbox.Text= "1"
    'instead of
    textbox.Text= textbox.text.Trim & "1"
     
  13. slavko

    slavko Member Licensed User

    Thanks for your extremly fast replies, i am not use to see so quick responding...

    it seems that the events are all ok. may be you can't see it in your textbox, because with every event, you are adding the number of your button to the end of your result string.
    With usage of break points, i can see, that during execution i am not reaching some button_click events. I noticed, that if i moved second line of buttons in first line (on display), now this line is triggered.

    Try to use
    Code:
    textbox.Text= "1"
    'instead of
    textbox.Text= textbox.text.Trim & "1"
    [/QUOTE]
    Did not help. I removed in all ten button events part with textbox.text.trim, still the same probem: button 1,2 and 3 are ok, others are not working (no click event).
    Reason for old code is, that i want to omit input of larger amounts (eg.321, by clicking button 3, than 2, than 1)... I can change code for this, but still i would like to avoid showing soft keyboard, because we would like to use this tablet for cycle counting in warehouse.
     
  14. eurojam

    eurojam Well-Known Member Licensed User

    slavko,
    try the attached project, which is yours. I only put all the other stuff (tables so on) as an comment. So the button events work properly. May be this is a starting point for you....

    good luck
    stefan
     

    Attached Files:

    DonManfred likes this.
  15. klaus

    klaus Expert Licensed User

    The probelm is what i suspected, pnlTable is covering the buttons.
    Only a part of btn1 to btn3 is not covered and therefore work if you click above the table.
    The solution depends on what exactly you want to do.
    If you want the buttons above the table you could add a panel onto the activity, after the table, and add the buttons onto this panel.
     
  16. slavko

    slavko Member Licensed User

    YES, that was the reason. Since Table was narrower , panel was too wide (but not visible) and buttons were visible, i did not realize that could be a reason. First line of buttons was positioned a bit higher than panel and they responsed....

    Thanks for suppport!!!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice