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.
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.
Compact buttons appear smaller but have a larger tappable area. The default tappable area is 48x48dp.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With