Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageButton -- can't get image in the center

Tags:

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?

like image 790
executor21 Avatar asked Mar 19 '09 00:03

executor21


People also ask

How do you make a picture fit in ImageButton?

How to programatically resize and show them? Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling. All of these attributes can be set in code on each ImageButton at runtime.

How can set image in center of button in Android?

Create a RelativeLayout "wrap_content" with the button image as the background or the button itself as the first element of the layout. Get a LinearLayout and set it to "layout_centerInParent" and "wrap_content". Then set your Drawable as an Imageview.

What is Android ImageButton?

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states.


2 Answers

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.

like image 101
executor21 Avatar answered Oct 20 '22 03:10

executor21


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

like image 26
Alnitak Avatar answered Oct 20 '22 04:10

Alnitak