Android Question StateListDrawable problem

Diabolus

Member
Licensed User
Longtime User
Hi.

I have problem with GradientDrawable (Enabled/pressed state).

I have 5 buttons that I create at runtime.

All the buttons i set the background to a GradientDrawable.

Now when I press any of the five buttons only the last created button getting pressed visually.
If I create let say 50 buttons and scroll and push a button sometimes they all gets pressed visually and sometimes only the last one. Never the one I press (except the last one :rolleyes: )

What's wrong? Is it a known bug?


I found the 'Gradient' code here in the forum.

B4X:
'###Gradient########################

Dim colsEnabled(2) As Int
colsEnabled(0) = Colors.RGB(60, 200, 80)
colsEnabled(1) = Colors.RGB(10, 100, 20)
   
' Define a GradientDrawable for Enabled state
Dim gdwEnabled As GradientDrawable
gdwEnabled.Initialize("TOP_BOTTOM",colsEnabled)
gdwEnabled.CornerRadius = 5
   
' Define two gradient colors for Pressed state
Dim colsPressed(2) As Int
colsPressed(1) = Colors.RGB(136, 136, 136)
colsPressed(0) = Colors.RGB(0, 0, 0)
   
' Define a GradientDrawable for Pressed state
Dim gdwPressed As GradientDrawable
gdwPressed.Initialize("TOP_BOTTOM",colsPressed)
gdwPressed.CornerRadius = 5
   
' Define a StateListDrawable
Dim stdGradient As StateListDrawable
stdGradient.Initialize
   
Dim states(2) As Int
states(0) = stdGradient.state_enabled
states(1) = -stdGradient.state_pressed
stdGradient.addState2(states, gdwEnabled)
   
Dim states(1) As Int
states(0) = stdGradient.state_pressed
stdGradient.addState2(states, gdwPressed)
   
'###################################


For I = 1 To 5

Dim Button1 As Button
Button1.Initialize("Button1")
Button1.Text = I
Button1.Background = stdGradient
Activity.AddView(Button1, 0dip, I * 50dip, 60dip, 40dip)

Next
 

klaus

Expert
Licensed User
Longtime User
Try this code:
B4X:
Sub SetGradientDrawable As SetGradientDrawable'###Gradient########################

Dim colsEnabled(2) As Int
colsEnabled(0) = Colors.RGB(60, 200, 80)
colsEnabled(1) = Colors.RGB(10, 100, 20)

' Define a GradientDrawable for Enabled state
Dim gdwEnabled As GradientDrawable
gdwEnabled.Initialize("TOP_BOTTOM",colsEnabled)
gdwEnabled.CornerRadius = 5

' Define two gradient colors for Pressed state
Dim colsPressed(2) As Int
colsPressed(1) = Colors.RGB(136, 136, 136)
colsPressed(0) = Colors.RGB(0, 0, 0)

' Define a GradientDrawable for Pressed state
Dim gdwPressed As GradientDrawable
gdwPressed.Initialize("TOP_BOTTOM",colsPressed)
gdwPressed.CornerRadius = 5

' Define a StateListDrawable
Dim stdGradient As StateListDrawable
stdGradient.Initialize

Dim states(2) As Int
states(0) = stdGradient.state_enabled
states(1) = -stdGradient.state_pressed
stdGradient.addState2(states, gdwEnabled)

Dim states(1) As Int
states(0) = stdGradient.state_pressed
stdGradient.addState2(states, gdwPressed)

'###################################
End Sub



For I = 1 To 5

Dim Button1 As Button
Button1.Initialize("Button1")
Button1.Text = I
Button1.Background = SetGradientDrawable
Activity.AddView(Button1, 0dip, I * 50dip, 60dip, 40dip)

Next
You must define a new GradientDrawable for each Button.
I put the code for the GradientDrawable in a subroutine which is called in the For / Next loop.
 
Upvote 0

Diabolus

Member
Licensed User
Longtime User
Thank you klaus.

Now it's working, I tried with your sub and changed a little..
Sub SetGradientDrawable As StateListDrawable

and added Return stdGradient at the bottom.


It feels strange that you need to define a new StateListDrawable to every button and can't use the same but who cares, now it works :) thanks again!
 
Last edited:
Upvote 0
Top