Reputation: 1169
How can I get an ImageButton
that has a fixed height and is only as wide as it needs according to the ratio of its source image?
I tried the following:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="8pt"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/list_download"
android:background="@android:color/black"
android:padding="0pt"/>
I set background to black just to see the boundaries of it exactly, and here is the outcome.
I have seen some solutions that suggest to use negative paddings but it is not an elegant solution. There should be a better one.
I tried similar configurations on an ImageView
too, but it also had extra padding. So it's not an ImageButton
specific problem (i.e. it is not related to this nine-patch issue).
EDIT: If I change scaleType
from fitCenter
to fitStart
, then the outcome is like this. So there is somehow a minimum width.
Upvotes: 1
Views: 2328
Reputation: 12222
you just need use android:scaleType="fitXY" and set padding 0dp.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="0dp"/>
Upvotes: 1
Reputation: 21
It's because you've set a limited height. That limits the image height, but when Android calculates the width of the content (the original unrezised image), it's wider.
You'll get the image without the black "padding" on the side if you use wrap_content for the height as well.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/list_download"
android:background="@android:color/black"
android:padding="0pt"/>
So, if you want the picture to be a specific size, you could resize it in your drawable folder to the size you want.
Upvotes: 2
Reputation: 625
you can put it in a Frame like so :
<FrameLayout
android:layout_width="wrap_content"
android:background="@android:color/black"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/list_download"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="8dp"/>
</FrameLayout>
it will be much easier to play with it like so
Upvotes: 0