Putting images in buttons

FFMan

Member
Licensed User
Longtime User
how do i load an image to a button. I've added the image to the activity in the designer but I can't see where to specify the image against the button.

thanks
 

susu

Well-Known Member
Licensed User
Longtime User
You can add image button directly in Designer. Just like this

attachment.php
 

Attachments

  • imagebutton.jpg
    imagebutton.jpg
    49.6 KB · Views: 4,890
Upvote 0

Stan

New Member
Licensed User
Longtime User
i don't have a field for image file

i don't have a field for image file (Button), why is my designer different??

Stan
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Under Button properties you have by default:
- Drawable : DefaultDrawable

You should click on DefaultDrawable and then select StatelistDrawable in the list.
Then you get three different states:
- Enabled Drawable
- Disabled Drawable
- Pressed Drawable

For each of these you have by default ColorDrawable.
Clicking on ColorDrawable gives you the choice between:
- ColorDrawable
- GradientDrawable
- BitmapDrawable

Select BitmapDrawable in the list and you'll get the Image file field.

Best regards.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Is there a way to programmatically change the button picture? so that after the button is pressed the picture is toggled to a new one?

For example a picture of stop being changed to go?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Erel,

I was able to achieve my aim with a single button but whe i tried an array it does not work
can you see where i have gone wrong

B4X:
      Dim bdwOn As BitmapDrawable
      bdwOn.Initialize(LoadBitmap(File.DirAssets, "Base ButtonP3.png"))
      bdwOff.Initialize(LoadBitmap(File.DirAssets, "bevel1.jpg"))
      Dim sld As StateListDrawable
      sld.Initialize
      sld.AddState(sld.State_Pressed, bdwOn)

   Dim Top As Int
   Top=20
   For x= 0 To 11
      Dim btnMaint As Button
      btnMaint.Initialize("btnMaintenance")
      btnMaint.Background=sld
      btnMaint.TextSize=36
      btnMaint.TextColor=Colors.Yellow
      btnMaint.Tag=x
      Activity.AddView(btnMaint,20,Top,200,80)
      Top=Top+82
   Next
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Ok I have the following code but it still does not work

B4X:
   Dim bdwOn As BitmapDrawable
   bdwOn.Initialize(LoadBitmap(File.DirAssets, "Base ButtonP3.png"))
   Dim Top As Int
   Top=20
   For x= 0 To 11
      Dim sld As StateListDrawable
      sld.Initialize
      sld.AddState(sld.State_Pressed, bdwOn)

      Dim btnMaint As Button
      btnMaint.Initialize("btnMaintenance")
      btnMaint.Background=sld
      btnMaint.TextSize=36
      btnMaint.TextColor=Colors.Yellow
      btnMaint.Tag=x
      Activity.AddView(btnMaint,20,Top,200,80)
      Top=Top+82
   Next

What am i still missing?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Erel,

If i dim the button as a toggle button it works. Is there a way to do an ordinary button?
 
Upvote 0

Marquin

New Member
I'm doing buttons in code, and don't know how to make a button have an image. I have done it with the designer but don't want to have to draw 36 buttons on designer and change each of their images when I have created code that makes all 36 buttons for me.

Would love to hear ideas and such.

Marquin
 
Upvote 0
Top