Android Question PB creating ToggleButtons with bitmaps

OliBee

New Member
Hi everyone,
I try to create togglebuttons with two différent bitmaps programmatically but nothing appears in my view, can someone tell me what is wrong in this code ?

Many Thanks

B4X:
Sub Globals
   Private lblTitre1 As Label
   Dim btOK1, btQuit1 As Button
   Dim btn(12) As ToggleButton 'boutons des quilles
   Dim bdwOn, bdwOff As BitmapDrawable 'images des boutons quilles
   Dim sld As StateListDrawable 'Définir les différents états des boutons quilles
   Dim BlEtatBouton As Boolean 'boulléen état d'un bouton quilles
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("loTour")
   'Initialisation des labels
   lblTitre1.Initialize("")
   lblTitre1.Text = "MY TITLE"

   'Initialisation des boutons d'action
   btQuit1.Text = "Quitter"
   btOK1.Text = "OK"
   'Initialisation des boutons quilles
   sld.Initialize
   Dim i As Int
   For i = 0 To 11
     btn(i).Initialize("Quille")
     btn(i).Tag = i
     bdwOn.Initialize(LoadBitmap(File.DirAssets, "Quille_12_Croix.png"))
     bdwOff.Initialize(LoadBitmap(File.DirAssets, "Quille_12.png"))
     btn(i).Background=sld
     'Initialiser le bouton sans texte dessus et par défaut en position off
     btn(i).TextOff = ""
     btn(i).TextOn = ""
     btn(i).Checked = False
     'Ajouter le bouton à la vue
     Activity.AddView(btn(i), 0%x, i * 30Dip, 15%x, 20Dip)
     BlEtatBouton = btn(i).Checked
   Next
 

klaus

Expert
Licensed User
Longtime User
You just initialize the StateListDrawable sld but you don't add any states.
You should have a look at this thread StateListDrawable example which has an example for a ToggleButton.
You need to Dim and initialize sld insides the For/Next loop !
 
Upvote 0

OliBee

New Member
Thank you Klaus, of course i had to add a state to StateListDrawable sld.

Here's the code now, it adds 12 buttons to the view, 4 buttons per lines with a different bitmap for each:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    'Déclaration des boutons et labels
    Dim btn(12) As ToggleButton 'boutons des quilles
    Dim bdwOn, bdwOff As BitmapDrawable 'images des boutons quilles
    Dim ln, rw As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("loTour")
  
    'Initialisation des boutons quilles  
    Dim i As Int
    For i = 0 To 11
        Dim sld As StateListDrawable 'Définir les différents états des boutons quilles
        sld.Initialize
        btn(i).Initialize("Quille")
        btn(i).Tag = i
bdwOn.Initialize(LoadBitmap(File.DirAssets, "Quille_" & (i+1) & "_Croix.png"))
        bdwOff.Initialize(LoadBitmap(File.DirAssets, "Quille_" & (i+1) & ".png"))
        sld.AddState(sld.State_Checked, bdwOn)
        sld.AddState(sld.State_Unchecked, bdwOff)
        btn(i).Background=sld
        'Initialiser le bouton sans texte dessus et par défaut en position off
        btn(i).TextOff = ""
        btn(i).TextOn = ""
        btn(i).Checked = False
        BlEtatBouton = btn(i).Checked
        'Ajouter le bouton à la vue, ' rangées de 3 boutons
        If (i mod 3) = 0 Then
            ln = 1
            rw = rw + 1
        Else
            ln = ln + 1  
        End If
        Activity.AddView(btn(i), (ln * 22%x), (rw * 70Dip), 15%x, 60Dip)
    Next
  
End Sub
 
Upvote 0
Top