Thanks for your code!
I was able to adapt it to my needs. Everything works fine.
However, I need help understanding how a section of the code works.
Here is the situation. I have 5 buttons:
Help Button (Button)
Exit Button (Button)
Button1 (Button)
Button2 (Button)
Text Button (Edit Text)
Not to complicate my explanation, I only need to understand the section that applies to Button1 and Button2, which are the only two buttons using the code.
With those two buttons (Button1, Button2), I need them to have: Focused - Enabled - Pressed - Disabled
I have four images, one for each state.
For Focused: Metal_Red_Brushed_01_1.jpg <---red copper
For Enabled: Brushed_Copper.jpg <---brown copper
For Pressed: Brushed_Steel_Blue_01.jpg <---blue copper
For Disabled: Brushed_Steel_Black_09.jpg <---black copper
On a phone or tablet, the default state for buttons is Enabled and it will show the corresponding brown copper image. If you tap (Pressed state) on that same button, then it will flicker for a moment with another image (blue copper). If the button is Disabled, then the black copper image will be shown.
On an Android TV, in order to see where the "cursor" is, I need the Focused state, which is the red copper image.
The code I need to understand how it works is this:
Dim states(4) As Int
states(0) = stdBitmap.state_Focused
states(1) = stdBitmap.state_Enabled
states(2) = -stdBitmap.state_Pressed
states(3) = -stdBitmap.state_Disabled
stdBitmap.addState2(states, bdwFocused)
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_Pressed
stdBitmap.addState2(states, bdwPressed)
btn.background=stdBitmap
Which is found at the bottom of this code:
Sub Buttondraw(btn As Button,btntxt As String,fns As Int,cl As Int)
Dim bx,by As Int
Dim bdwFocused As BitmapDrawable
Dim bdwEnabled As BitmapDrawable
Dim bdwPressed As BitmapDrawable
Dim bdwDisabled As BitmapDrawable
buttonlabel.Text=btntxt
buttonlabel.TextSize=fns
Dim Height_, Width_ As Int
buttonlabel.Width = -2
buttonlabel.Height = -2
DoEvents
obj.Target = buttonlabel
Width_ = obj.RunMethod("getWidth")
Height_ = obj.RunMethod("getHeight")
buttonpic.Width=btn.Width
buttonpic.Height=btn.Height
'================This will show text on the button
' buttonpic.Bitmap = LoadBitmap(File.Dirassets,"Brushed_Steel_Blue_01.jpg") '?????? Not needed
c.Initialize(buttonpic)
bx=(btn.Width-Width_)/2
by=(btn.Height+fns)/2
c.DrawText(btntxt,bx,by,myfont, fns, cl, "LEFT")
'================
'This section inludes: FOCUSED, ENABLED, PRESSED, & DISABLED
bdwEnabled.Initialize(buttonpic.Bitmap) 'It will not work without this code HERE.
Dim Focused As BitmapDrawable
buttonpic.Bitmap = LoadBitmap(File.Dirassets,"Metal_Red_Brushed_01_1.jpg")
c.Initialize(buttonpic)
c.DrawText(btntxt,bx,by,myfont, fns, cl, "LEFT")
bdwFocused.Initialize(buttonpic.Bitmap)
bdwEnabled.Initialize(buttonpic.Bitmap) 'It will not work without this code HERE.
Dim bdwEnabled As BitmapDrawable
buttonpic.Bitmap = LoadBitmap(File.Dirassets,"Brushed_Copper_06.jpg")
c.Initialize(buttonpic)
c.DrawText(btntxt,bx,by,myfont, fns, cl, "LEFT")
bdwEnabled.Initialize(buttonpic.Bitmap)
Dim bdwPressed As BitmapDrawable
buttonpic.Bitmap = LoadBitmap(File.Dirassets,"Brushed_Steel_Blue_01.jpg")
c.Initialize(buttonpic)
c.DrawText(btntxt,bx,by,myfont, fns, cl, "LEFT")
bdwPressed.Initialize(buttonpic.Bitmap)
Dim bdwDisabled As BitmapDrawable
buttonpic.Bitmap = LoadBitmap(File.Dirassets,"Brushed_Steel_Black_09.jpg")
c.Initialize(buttonpic)
c.DrawText(btntxt,bx,by,myfont, fns, Colors.Gray, "LEFT")
bdwDisabled.Initialize(buttonpic.Bitmap)
Dim stdBitmap As StateListDrawable
stdBitmap.Initialize
Dim states(4) As Int
states(0) = stdBitmap.state_Focused
states(1) = stdBitmap.state_Enabled
states(2) = -stdBitmap.state_Pressed
states(3) = -stdBitmap.state_Disabled
stdBitmap.addState2(states, bdwFocused)
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_Pressed
stdBitmap.addState2(states, bdwPressed)
btn.background=stdBitmap
End Sub
FYI: You call upon the Sub BottonDraw in the Sub Activity_Create(FirstTime As Boolean) with this other code that puts it onto the button:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("myappmain")
'==========This section calls the "Sub BottonDraw..."
myfont = Typeface.LoadFromAssets("ardelaney.ttf")
buttonpic.Left=3000
buttonlabel.Left=3000
Buttondraw(Button1,"",18,Colors.Black) 'NOTE: This button's tile was added in the designer as an alternative
Buttondraw(Button2,"B2",18,Colors.Black)
'Usage : Buttondraw(Button_name ,Button Text,Font size,Font color)
'==========
End Sub
Also, why is it that this code will not work on an exit routine button like this one:
Sub Exit_Click
ExitApplication
End Sub
If the button is called "Exit", the "Sub Buttondraw" will work, but the Exit button does nothing, it will not exit the app... but the bottom has to be called "Exit" for the "ExitApplication" to execute properly!!! Don't apply the Sub Buttondraw to the Exit button and it works as intended, it exits the app. Why?
Also, with this code, how do you go on adding a border, of any color, to the buttons?
Can you please help me understand this? Any one?
Thanks in advance.