Custom Button Colors

Bill Norris

Active Member
Licensed User
Longtime User
This is a modified sample of code previously posted, to demonstrate how to manually set customized button colors. I have two buttons on the activity, button1 and button2.

It works fine when just using button1. However, if you add to end of the code

button2.Background = stdGreenColor

Strange behavior occurs. If I touch button1, button2 changes color but botton1 does not.

My ultimate goal is to apply a custom gradient to all of the buttons on the activity. This code does not use gradients but the basic principles are the same.




B4X:
dim button1 as button
dim button2 as button

   Dim cdGreenColorEnabled As ColorDrawable
  cdGreenColorEnabled.Initialize(Colors.yellow,10)
  ' Define a color for Pressed state
  Dim cdGreenColorPressed As ColorDrawable
  cdGreenColorPressed.Initialize(Colors.Red,10)
  ' Define a StateListDrawable
  Dim stdGreenColor As StateListDrawable
  stdGreenColor.Initialize
  Dim states(2) As Int
  states(0) = stdGreenColor.state_enabled
  states(1) = -stdGreenColor.state_pressed
  stdGreenColor.addState2(states, cdGreenColorEnabled)
  Dim states(1) As Int
  states(0) = stdGreenColor.state_pressed
  stdGreenColor.addState2(states, cdGreenColorPressed)
  ' Set stdGreenColor to button background 
 button1.Background = stdGreenColor


Thanks for any assistance.
 

Bill Norris

Active Member
Licensed User
Longtime User
Yes, I seem to have discovered that. I was able to define the statelistdrawable object as an array, then assign each button's background to a different element of the array. Works like a charm.

Thanks, Klaus
 
Upvote 0
Top