Changing keyboard button images

Guardian17

Active Member
Licensed User
Longtime User
I have code to generate my own QWERTY keyboard on screen. The game that this will be part of will require the keyboard button images (pressed and unpressed) to change throughout the game.

The generation of the keyboard is not a problem. The question is, given the code below that generates the key buttons, is it possible to re-access the "button()" array and change individual Enabled and Pressed images for any specific button? Any key that needs to be changed will be accessed by an index into the array (0-25) according to the order that they were generated.

Here's the code to generate the keyboard:

B4X:
   'Place keyboard on screen
   For i = 0 To 25
      If i<10 Then
         OffsetX = (100%x - (10-i) * (Width-3) - 10)
         OffsetY = 100%y - Width * 4 + 20
      Else If i<19 Then
         OffsetX = (100%x - (20-i) * (Width-3) + 8)
         OffsetY = 100%y - Width * 3 + 13
      Else
         OffsetX = (100%x - (28-i) * (Width-3) - 3)
         OffsetY = 100%y - Width * 2 + 6
      End If
      Dim b As Button
      b.Initialize("button")
      Activity.AddView(b,OffsetX, OffsetY, Width, Width-5)

      ' Define a bitmap for Enabled state
      Dim bdwEnabled As BitmapDrawable
      bdwEnabled.Initialize(LoadBitmap(File.DirAssets, "btnunpressed20x15.png"))
      ' Define a bitmap for Pressed state
      Dim bdwPressed As BitmapDrawable
      bdwPressed.Initialize(LoadBitmap(File.DirAssets, "BtnPressed20x15.png"))
      ' 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)
      ' Set stdBitmap to button btnBitmap
      b.Background = stdBitmap
      b.Typeface = Typeface.MONOSPACE
      b.TextSize = 14 'was 12
      b.Text = KeyLegends.SubString2(i,i+1)
      b.Tag = Asc (b.Text)-65
      b.TextColor = Colors.Black
      Button(i) = b
   Next

"KeyLegends" is defined as a string as:
"QWERTYUIOPASDFGHJKLZXCVBNM",
and I'm currently using only a 320x480 variant.

The other keyboard background images will be:
btnunpressed20x15Hilite.png, btnPressed20x15Hilite.png
and
btnunpressed20x15Elim.png, btnPressed20x15Elim.png

Thanks in advance for your help.
 

Guardian17

Active Member
Licensed User
Longtime User
Thank you

Thanks, Erel. I'll give it a go later today. Just wanted to be sure it was possible, and you pointed me in the right direction.
 
Upvote 0
Top