iOS Tutorial Programmatic changing of button images

For my latest app, Less Note 1.0.0 (currently WFR), I wanted to show when a particular colored pen or the eraser was active and the others inactive. So, I used Photoshop to create glowing "active" buttons.

Much of this (isn't it always the case? :rolleyes:) is gleaned from snippets of others' very generous contributions of code.

1) Easiest to use the designer to create your base layout, including the starting and pressed button images.
2) Use an image editor like Photoshop to create a third button style.
3) Add the third button image to the list of files in the IDE.
4) Here's the code I used in my app to highlight the active pen (black one in this case), and set the inactive pens to non glow versions:
B4X:
Sub BlkBut_Click
    penColor = Colors.Black
    penwidth = 2
    bmpGlow.Initialize(File.DirAssets, "BlkButton_Glow.png")
    SetBackgroundImage(BlkBut, bmpGlow, 0)
    bmpNoGlow.Initialize(File.DirAssets, "RedButton.png")
    SetBackgroundImage(RedBut, bmpNoGlow, 0)
    bmpNoGlow.Initialize(File.DirAssets, "BlueButton.png")
    SetBackgroundImage(BlueBut, bmpNoGlow, 0)
    bmpNoGlow.Initialize(File.DirAssets, "GreenButton.png")
    SetBackgroundImage(GreenBut, bmpNoGlow, 0)
    bmpNoGlow.Initialize(File.DirAssets, "EraseButton.png")
    SetBackgroundImage(eraseBut, bmpNoGlow, 0)
End Sub
Note that the manually (IE - not with Designer) added button images are located in File.DirAssets.
5) SetBackgroundImage changes the button images (thanks Erel for this code from another thread):
B4X:
'state: 0 - normal, 1 - highlighted, 2 - disabled
Sub SetBackgroundImage(b As Button, bmp As Bitmap, state As Int)
    Dim no As NativeObject = b
    no.RunMethod("setBackgroundImage:forState:", Array(bmp, state))
End Sub
6)That's it!
 
Top