8

I have been working on interfaces lately and I have many problems with imagebutton sizes. I'm very bad at designing stuff and I'm having major problems with the Android Icons. Thus, I have some questions regarding those.

First of all this is my current layout:

enter image description here

As you can see, I have two icons that have different sizes and that's not good. I can't seem to force them to take the same size.

My questions are:

1 - How do I force them to take the same size?

2 - What are the recommended size differences for hdpi, ldpi,mdpi and xhdpi?

3 - Why, if I set the imageButton background color to the same color as the layout background, the Icon is moved as if there was no button in the background?

Example: enter image description here

Thanks in advance!!

Loic O.
  • 486
  • 2
  • 7
  • 22

5 Answers5

9

For these ImageButtons set the images using (instead of src):

android:background="@drawable/image_name"

Next thing is set:

android:layout_height="10dp" //or any size required
android:layout_width="10dp" //or any size required ... let these 2 sizes be same for both the ImageButtons

"dp" or "dip" is device independent pixels. The android system will take care of the resizing for different screen resolutions

Also, a better thing to do would be to set-up hover images for your button separately.

pixelscreen
  • 1,945
  • 2
  • 18
  • 40
  • Thanks! I was able to do the trick with this! although, what do you mean by set-up hover images? – Loic O. Jul 18 '12 at 11:57
  • Glad I was able to help :) When the user presses the button / when the button is in the "pressed" state - A hover image will be displayed indicating the button press. For more on how to do it: http://stackoverflow.com/q/606694/735675 – pixelscreen Jul 18 '12 at 13:18
5

Instead of replacing android:src with android:background you can use android:scaleType attribute and set it to "fitXY".

see: Android - Image Button Scale

muiz6
  • 211
  • 4
  • 5
1

type this:

in java:

YOUR_VIEW.setLayoutParams(new LayoutParams(width,height));

or in XML:

android:layout_height="10px"
android:layout_width="10px"
bmavus
  • 892
  • 1
  • 7
  • 21
1
android:layout_height="10dp"

android:layout_width="10dp"

Dont keep in px, always you have to keep in only dp

sfmirtalebi
  • 370
  • 7
  • 16
shassss
  • 321
  • 1
  • 13
0

As the other answers have said, set the button sizes using dp or dip in the layout xml files.

For handling different screen sizes see this doc: http://developer.android.com/guide/practices/screens_support.html

ScouseChris
  • 4,377
  • 32
  • 38