h4lc0n
h4lc0n

Reputation: 2770

imagebutton set image size

So imagine you have several Buttons on your layouts. The project is almost finished and there's a lot of dynamic stuff (programmatically setting images, listeners, statedrawables, etc. etc.).

Now, you're told that the buttons are kinda tricky to click so your first thought (at least mine) is "ok, I'll just make the bounding box bigger". To do this, I just need to give those Buttons more width and height so they will be easier to click.

My problem comes when I see that those Buttons are using background to store the image and so, whenever I make them bigger so does the image inside.

My question is, am I stuck with having to create a layout on top of it, assign the listener to this new, bigger layout, and then leaving the button as an image, or is there any easier way?

                                             /¯¯¯¯¯¯¯¯¯¯¯¯\ <- outter layout
                   /¯¯¯¯¯¯¯¯\                | /¯¯¯¯¯¯¯¯\ |
                   |        |                | |        | |
                   | BUTTON |      --->      | | BUTTON | |
                   |        |                | |        | |
                   \________/                | \________/ |
                                             \____________/

Thanks

Upvotes: 5

Views: 644

Answers (3)

Italo Borssatto
Italo Borssatto

Reputation: 15679

Edit all your buttons and set the layout_margin or the padding to the space you need.

Example:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:padding="20dp"
    android:text="Button" />

Upvotes: 0

edwin
edwin

Reputation: 8081

Use this in your xml layout

android:background="@null"

This will help you to show you button as the image only

Upvotes: 1

h4lc0n
h4lc0n

Reputation: 2770

Nevermind, found the answer, I'll just leave it here in case anyone else is having this issue.

In our case we couldn't afford to edit the images, what I did is change the Button node to an ImageButton. Now, instead of using background we use src and, once the ImageButton is enlarged, a simple inside-padding is added so the image maintains the original size even though the button itself is actually bigger and, therefore, easier to click.

On a side note, if your previous background had alpha you might want to do this now:

android:background="@null"

Upvotes: 1

Related Questions