Android Question Disabled BitmapDrawable Button

pin71

Member
Licensed User
Hi,

I have this code to create 4 buttons :

B4X:
For y = 0 To 3
        Butt.Initialize("ButtonBT") 'All buttons share the same event sub
        Butt.TextSize = testob
        Panel5.AddView(Butt,800, 100+150* y, 300dip, 110dip)
        ButtonBT(y+1) = Butt 'store a reference to this view
        Butt.Tag =y+1
        ButtonE(y+1).Initialize(LoadBitmap(File.DirAssets , "gen_butt.jpg"))
        ButtonE(y+1).Gravity =Gravity.NO_GRAVITY
        ButtonP(y+1).Initialize(LoadBitmap(File.DirAssets , "gen_buttP.jpg"))
        ButtonBitmap(y+1).Initialize
        Dim states(2) As Int
        states(0) = ButtonBitmap(y+1).state_enabled
        states(1) = -ButtonBitmap(y+1).state_pressed
        ButtonBitmap(y+1).addState2(states, ButtonE(y+1))
        Dim states(1) As Int
        states(0) = ButtonBitmap(y+1).state_enabled
        ButtonBitmap(y+1).addState2(states, ButtonP(y+1))
        ' Set stdBitmap to button b1
        ButtonBT(y+1).Background  = ButtonBitmap(y+1)
        ButtonBT(y+1).Text = mess(y+52)
    Next

How can I set the disabled image?

Thanks
 

LucaMs

Expert
Licensed User
I use this routine:
B4X:
Private Sub ButtonSetStateListDrawable(btnBitmap As Button, EnabledBitmap As Bitmap, PressedBitmap As Bitmap, DisabledBitmap As Bitmap)
    ' Define a bitmap for Enabled state
    Dim bdwEnabled As BitmapDrawable
    bdwEnabled.Initialize(EnabledBitmap)
  
    ' Define a bitmap for Pressed state
    Dim bdwPressed As BitmapDrawable
    bdwPressed.Initialize(PressedBitmap)

    ' Define a bitmap for Disabled state
    Dim bdwDisabled As BitmapDrawable
    bdwDisabled.Initialize(DisabledBitmap)

    ' Define a StateListDrawable
    Dim stdBitmap As StateListDrawable
    stdBitmap.Initialize
    Dim states(2) As Int
    states(0) = stdBitmap.state_enabled
    states(1) = -stdBitmap.state_pressed
    stdBitmap.addState2(states, bdwEnabled)
    Dim states(1) As Int
    states(0) = stdBitmap.state_enabled
    stdBitmap.addState2(states, bdwPressed)
    Dim states(1) As Int
    states(0) = stdBitmap.State_Disabled
    stdBitmap.addState2(states, bdwDisabled)

    ' Set stdBitmap to button btnBitmap
    btnBitmap.Background = stdBitmap
End Sub

You can pass your buttons and their images to it.
 
Upvote 0

pin71

Member
Licensed User
Another question:

How can I disable some of the buttons?

Refering to my code above I have created an array of buttons and if I want to disable some of them it doesn't work. Only the last one get disabled.

I'm doing :

buttonBT(3).enable = False
buttonBT(4).enable = False

but only the last one get disabled.

What am I doing wrong ?

Thanks
 
Upvote 0

LucaMs

Expert
Licensed User
We can not understand it only seeing those two lines.

However, you can try to use the Invalidate method.
 
Last edited:
Upvote 0

pin71

Member
Licensed User
Here is the code I've used to generate 4 buttons :

B4X:
    For y = 0 To 3
        Butt.Initialize("ButtonBT") 'All buttons share the same event sub
        Butt.TextSize = testob
        Panel5.AddView(Butt,800, 100+150* y, 300dip, 110dip)
        ButtonBT(y+1) = Butt 'store a reference to this view
        Butt.Tag =y+1
        ButtonE(y+1).Initialize(LoadBitmap(File.DirAssets , "gen_butt.jpg"))
        ButtonE(y+1).Gravity =Gravity.NO_GRAVITY
        ButtonP(y+1).Initialize(LoadBitmap(File.DirAssets , "gen_buttP.jpg"))
        ButtonD(y+1).Initialize(LoadBitmap(File.DirAssets , "gen_buttD.jpg"))
        ButtonBitmap(y+1).Initialize
        Dim states(2) As Int
        states(0) = ButtonBitmap(y+1).state_enabled
        states(1) = -ButtonBitmap(y+1).state_pressed
        ButtonBitmap(y+1).addState2(states, ButtonE(y+1))
        Dim states(1) As Int
        states(0) = ButtonBitmap(y+1).state_enabled
        ButtonBitmap(y+1).addState2(states, ButtonP(y+1))
       
        Dim states(1) As Int
        states(0) = ButtonBitmap(y+1).State_Disabled
        ButtonBitmap(y+1).addState2(states, ButtonD(y+1))
       
        ' Set stdBitmap to button b1
        ButtonBT(y+1).Background  = ButtonBitmap(y+1)
        ButtonBT(y+1).Text = mess(y+52)
       
    Next

I have then 4 buttons. ButtonBT(1), ButtonBT(2), ButtonBT(3) and ButtonBT(4) .

If I set ButtonBT(2).enable = False only the ButtonBT(4) will switch the image to disabled.
 
Upvote 0

LucaMs

Expert
Licensed User
you should declare each button into the loop, to get 4 different buttons.

Also, you can simply call my the sub i posted.
 
Upvote 0
Top