Android Question button with image

Discussion in 'Android Questions' started by bogdanc, Mar 23, 2015.

  1. bogdanc

    bogdanc Active Member Licensed User

    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.

  2. walterf25

    walterf25 Well-Known Member Licensed User

    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.
    bogdanc likes this.
  3. bogdanc

    bogdanc Active Member Licensed User

    I did like this

    Dim bmp0 As BitmapDrawable
    LoadBitmap(File.DirAssets, "my_img.png"))
  4. walterf25

    walterf25 Well-Known Member Licensed User

    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!
    Peter Simpson and bogdanc like this.
  5. klaus

    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 and walterf25 like this.
  6. bogdanc

    bogdanc Active Member 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:

    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)
  7. walterf25

    walterf25 Well-Known Member Licensed User

    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)

  8. klaus

    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.
    bogdanc likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice