24

I want to create an Android ImageButton in the xml file with a background image, and a smaller icon on top of that, right in the center. For some reason, it's not obvious how to do it, and documentation is no help.

<ImageButton
android:id="@+id/sound_button"
android:layout_x="430px"
android:layout_y="219px"
android:layout_width="48px"
android:layout_height="48px"
android:scaleType="center"
android:src="@android:drawable/volumeicon"
android:background="@drawable/clearbuttonup"
/>

However, no matter what I try (putting "wrap _ content" instead of absolute numbers on layout _ height and layout _ width, the icon remains in the top left corner of the button. If, however, I have no background image (i.e. a system default white button), the icon does go into the center. The button also goes into the top left corner when I use a color instead of an image for the background.

Why does this happen, and how would I actually get the behavior I want-- that is, a background image with the icon in the center?

Nikhil
  • 16,194
  • 20
  • 64
  • 81
executor21
  • 4,532
  • 6
  • 28
  • 38

3 Answers3

17

I'm a real moron. That's how it appears in the display the Ecliplse plugin provide side-by-side with the XML editor. When I actually build the project and ran it on a device, it displayed everything correctly.

executor21
  • 4,532
  • 6
  • 28
  • 38
  • I had this experience too... fretted over it for ages then tried it out on my phone and it displayed fine. I wonder where we can report these bugs. – Jeremy Logan May 05 '09 at 20:48
  • "Ecliplse plugin provide side-by-side with the XML editor." are you saying that the Eclipse plugin has a designer in the IDE? Have I missed something – Pat Long - Munkii Yebee Jul 07 '10 at 10:31
1

Ironically I had exactly the opposite problem with an ImageView - I couldn't get it left-aligned no matter what I tried. In this case I was using layout_width="fill_parent".

See my question here: Android layout - alignment issue with ImageView

Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

try use the Button and set the background it's very useful and easy :D

make the text empty and you can use it like image button ^_^

Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19