Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting the size of an ImageButton in Android

Tags:

Is there any way to do this? I have tried padding the image and setting the width/height of the view, but neither seems to work. Here is an example:

<ImageButton     android:id="@+id/search"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:src="@drawable/search_small"     android:paddingTop="4sp"     android:paddingBottom="4sp"     android:paddingLeft="6sp"     android:paddingRight="6sp"     android:layout_marginRight="10sp"     android:layout_marginTop="6sp"     android:layout_marginBottom="6sp"     android:layout_alignParentRight="true" /> 

I want the button to be wider than it is tall, but it is coming out the other way round.

like image 286
Philip Sheard Avatar asked Aug 01 '11 13:08

Philip Sheard


People also ask

How do you scale 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.

What should be size of button in android?

Compact buttons appear smaller but have a larger tappable area. The default tappable area is 48x48dp.

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

Just had a play to try and understand your problem.

Seems ImageButton is a composite view which has a few pre-set values. Such as some sort of margin which you cannot override with the XML. If you cannot change your image to match what you want to happen then you are better to create your own composite view.

Here is my example of a composite view you can make yourself:

<FrameLayout android:layout_width="wrap_content"               android:layout_height="wrap_content">               <Button android:id="@+id/saveSearchButton"               android:layout_width="50dp"                      android:layout_height="50dp" />     <ImageView android:layout_width="45dp"                  android:layout_height="45dp"                android:scaleType="fitXY"                android:src="@drawable/ic_menu_save"                 android:layout_gravity="center"/> </FrameLayout>  <FrameLayout android:layout_width="wrap_content"               android:layout_height="wrap_content">               <Button android:id="@+id/clearSearchButton"             android:layout_width="50dp"              android:layout_height="50dp" />     <ImageView android:layout_width="45dp"                android:layout_height="45dp"                android:scaleType="fitXY"                 android:src="@drawable/ic_menu_close_clear_cancel"                 android:layout_gravity="center"/> </FrameLayout>       

And the original buttons:

<ImageButton android:id="@+id/imageButton1"                android:src="@drawable/ic_menu_save"                             android:layout_height="45dp" android:layout_width="45dp"/>  <ImageButton android:id="@+id/imageButton2"              android:src="@drawable/ic_menu_close_clear_cancel"               android:layout_height="45dp"              android:layout_width="45dp"/>   

Here we can see custom image/button composite followed by the build in ImageButton as part of the SDK:

Example

like image 76
Graeme Avatar answered Oct 05 '22 16:10

Graeme


Set android:background instead of android:src to set the image on the button. This will adjust the image to your button's size. Then adjust the padding after.

like image 41
C.O Avatar answered Oct 05 '22 18:10

C.O