Android Question button with image

bogdanc

Active Member
Licensed User
Hello!
I need to make a button with some icon.

I think to make a label with background of my image and make an action on click.
I know that I can use a bitmap as background of my image.
But how change the img on diffrent action on the button.

For the image I use a png file.

button_close.png
 

walterf25

Expert
Licensed User
Hello!
I need to make a button with some icon.

I think to make a label with background of my image and make an action on click.
I know that I can use a bitmap as background of my image.
But how change the img on diffrent action on the button.

For the image I use a png file.

View attachment 33030
It is easier if you use the designer, there you can upload both images, the normal button image and the pressed button image, you can assign each image to each event.

Hope this helps.
Walter
 

bogdanc

Active Member
Licensed User
I did like this

B4X:
    Dim bmp0 As BitmapDrawable
     bmp0.Initialize(LoadBitmap(File.DirAssets, "my_img.png"))
    Button2.Background=bmp0
    Button2.Height=90dip
    Button2.Width=90dip
 

walterf25

Expert
Licensed User
I did like this

B4X:
    Dim bmp0 As BitmapDrawable
     bmp0.Initialize(LoadBitmap(File.DirAssets, "my_img.png"))
    Button2.Background=bmp0
    Button2.Height=90dip
    Button2.Width=90dip
That works but you will need to load two different images, one for when the button is normal, (Unpressed) and another one for when the button is pressed!
 

klaus

Expert
Licensed User
What exactly do you want to do ?
What states is your 'button' supposed to have ?
If it's only enabled and pressed use the solution suggested by walterf25.
You may have a look at chapter 10.1.4 StateListDrawable in the User's Guide.
Otherwise you need to handle different states yourself.
In this case I would suggeat panels and use the Touch event.
I have used this in the GPSExample program which is included in the User's Guide.
 

bogdanc

Active Member
Licensed User
What exactly do you want to do ?
What states is your 'button' supposed to have ?
If it's only enabled and pressed use the solution suggested by walterf25.
You may have a look at chapter 10.1.4 StateListDrawable in the User's Guide.
Otherwise you need to handle different states yourself.
In this case I would suggeat panels and use the Touch event.
I have used this in the GPSExample program which is included in the User's Guide.
Thank You for the answer.

I have experience with many designing programs and always I work on Layers to put some objects.

So I think to make like this my button:
button_b4a_question.png




Layer 1 as Container to keep everything
Layer 2 button background to modify a color on different action
Layer 3 to contain a graphic png icon (here also modify of the png )

My question is how put everything together.
Use a label/ panel for each layer and add a background to it?
But how to gen an action on click on Layer 1 if on the top a I have to layers (label/panel)
 

walterf25

Expert
Licensed User
Thank You for the answer.

I have experience with many designing programs and always I work on Layers to put some objects.

So I think to make like this my button:
View attachment 33054



Layer 1 as Container to keep everything
Layer 2 button background to modify a color on different action
Layer 3 to contain a graphic png icon (here also modify of the png )

My question is how put everything together.
Use a label/ panel for each layer and add a background to it?
But how to gen an action on click on Layer 1 if on the top a I have to layers (label/panel)
I understand now what you are trying to do, however I think you need to shake the graphic designing mentality off, B4A is not a graphic designing software, although i'm sure you could do something similar to what you are trying to do here, I think it is actually a lot simpler than you are thinking.

First of all your layer 1, could be a panel, that would be the container where you can place your other views.
I think you could get rid of layer 2, since layer 3(button) can hold the two different images for when you press on the button, or when the button is in normal mode(unpressed)

Cheers,
Walter
 

klaus

Expert
Licensed User
You could mange everything with 1 panel using the Touch event to handle the different states and change the panel background image according to the states.
For this you need to combine your layer images.
This is what I did in the GPSExample program, if I remember well 4 images for 1 'button' two functions and two states per function.
You could also have two panels and playing with their backgrounds.
The back panel with the colors and the top panel with the 'button' image and a transparent surrounding using only the back panel touch event.
 
Top