Android Question Button.SetBackgroundImage and button.background

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I'm trying to set a button with a color on the background and also an image.
But the only possible choice is the one or the other? Or I can set them both?

B4X:
Dim bmpTeam As Bitmap
Dim green As ColorDrawable

bmpTeam.Initialize ( File.DirAssets , "team.png")
green.Initialize(Colors.RGB(8, 125, 0), 10dip)

bt.Initialize("bt")
Activity.AddView(bt, Left1, Top1, btWidth, btHeight)
bt.Background = green
bt.SetBackgroundImage(bmpTeam)
 

DonManfred

Expert
Licensed User
Longtime User
You cannot set both at once i think.. But you can put an image with the right background-color as backgroundimage :)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I am thinking the same as you and Don. You can only have one or the other. But, how about the below code for an option. When pressed the button is green, when not pressed it has the image as the background. I stand corrected:
B4X:
Sub Globals
    Dim bt As Button
    Dim sld As StateListDrawable
    Dim bmpTeam  As BitmapDrawable
    Dim green As ColorDrawable
End Sub

Sub Activity_Create(FirstTime As Boolean)
    bt.Initialize("bt")
    green.Initialize(Colors.RGB(8, 125, 0), 10dip)
    bmpTeam.Initialize(LoadBitmap(File.DirAssets,"team.png"))
    sld.Initialize
    sld.AddState(sld.State_Disabled, bmpTeam )  'when Not pressed
    sld.AddState(sld.State_Pressed,green )      'when pressed
    bt.Background = sld
    bt.Text="Click me"
    bt.TextColor=Colors.White
    bt.TextSize=24
    Activity.AddView(bt, 0, 0, 30%x, 75dip)
End Sub
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I am thinking the same as you and Don. You can only have one or the other. But, how about the below code for an option. When pressed the button is green, when not pressed it has the image as the background. I stand corrected:
B4X:
Sub Globals
    Dim bt As Button
    Dim sld As StateListDrawable
    Dim bmpTeam  As BitmapDrawable
    Dim green As ColorDrawable
End Sub

Sub Activity_Create(FirstTime As Boolean)
    bt.Initialize("bt")
    green.Initialize(Colors.RGB(8, 125, 0), 10dip)
    bmpTeam.Initialize(LoadBitmap(File.DirAssets,"team.png"))
    sld.Initialize
    sld.AddState(sld.State_Disabled, bmpTeam )  'when Not pressed
    sld.AddState(sld.State_Pressed,green )      'when pressed
    bt.Background = sld
    bt.Text="Click me"
    bt.TextColor=Colors.White
    bt.TextSize=24
    Activity.AddView(bt, 0, 0, 30%x, 75dip)
End Sub

Many thanks for your code, but I prefer both background :)
 
Upvote 0
Top