Button Bitmap image Pressed Drawable

agraham

Expert
Licensed User
Longtime User
I don't think you can change the existing one but you can replace it.
B4X:
   Dim gd1 As GradientDrawable
   Dim gd2 As GradientDrawable
   Dim cols(2) As Int
   cols(0) = Colors.Cyan
   cols(1) = Colors.Red
   gd1.Initialize("TL_BR",cols)
   gd2.Initialize("TR_BL",cols)
   Dim states(1) As Int
   Dim sd As StateListDrawable
   sd.Initialize
   Dim states(2) As Int
   states(0) = sd.state_enabled
   states(1) = -sd.state_pressed
   sd.addState(states, gd1)
   Dim states(1) As Int
   states(0) = sd.state_pressed
   sd.addState(states, gd2)
   Button1.Background = sd
The order the states are added is important because the Android state matching algorithm is a simple linear search. Unfortunately I can't get a state to show for the unpressed condition so the button is black until pressed. Most likely I'm not understanding something :( or the StateListDrawable is missing a state constant. Erel did say it wasn't finished. I've tried various integer constants to define the unpressed state but can't hit on the right one.

EDIT:- I've got it work and changed the code above but I have to admit I don't understand why it needs the enabled and unpressed combination to display the unpressed state instead of just the unpressed (-ve) state when it doesn't appear to need it for the pressed state.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Isn't the Android documentation wonderful compared to .NET Not!

I found this in the documentation for a related method "setState" and it seems to me it might hold (undocumented!) for addState as well.

"Note: The Drawable holds a reference on to stateSet until a new state array is given to it, so you must not modify this array during that time."

I was reusing the state array passed to addState. By Dimming a new states(1) array I got it to work with just "states(0) = -sd.state_pressed".
 

klaus

Expert
Licensed User
Longtime User
Thank you Andrew,

It's not exaclty what I wanted to do but you showed me the way how to do it ! :)
I wanted to change the bitmaps to simulate a ToggleButton, when pressing the bitmaps change and remain when unpressed and on the next press.
Attached a small test program for others who might be interested in.
Playing with 3 buttons Bitmap-, Gradient-, Color Drawable

Best regards.

EDIT: Updated the file.
 
Last edited:

Cor

Active Member
Licensed User
Longtime User
Hello,

the TestButtons.b4a file is empty

only the standard definitions

grCor
 

Cor

Active Member
Licensed User
Longtime User
Works on HTC Magic 1.6

ps
For running on device, please add graphics to files tab

grCor
 
Top