Fit Image in Imagebutton in Android

Fit Image in ImageButton in Android

I want them to cover 75% of the button area.

Use android:padding="20dp" (adjust the padding as needed) to control how much the image takes up on the button.

but where as some images cover less area, some are too big to fit into the 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. However, it is much easier to set and preview in xml in my opinion.

Also, do not use sp for anything other than text size, it is scaled depending on the text size preference the user sets, so your sp dimensions will be larger than your intended if the user has a "large" text setting. Use dp instead, as it is not scaled by the user's text size preference.

Here's a snippet of what each button should look like:

    <ImageButton
android:id="@+id/button_topleft"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitCenter" />

Sample Button

how can i resize imageButton?

First try to use android:background instead of android:src for setting the image on the button.In most cases,it helps.If not,then see these..

  • ImageButton in Android
  • Resize ImageButton?

Fit image on ImageButton

If the orientation of linear layout is horizontal then while setting weight to its child, you should set its width="0dp" and for vertical orientation height=0dp. and use fitXY.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<ImageButton
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />

<ImageButton
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />

<ImageButton
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />

</LinearLayout>

Hope this will help you.

Android - Image Button Scale

That's a good try, you are trying to go deeper into the android implementations.

Ans #3:

At first providing exact size as height and width is not a good practice in android. If you wish to know why, the reason would be simply it will try to squeeze/expand the image to the size specified by you in the height and width params. Always wrap_content is the preferred Parameter so that the OS will automatically adjust depending upon the screen size.Hence you need to modify as:

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibTest"
android:src="@drawable/image_top_test"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:layout_below="@+id/hlTopBar"
android:background="#00000000"
android:scaleType="fitXY" />

Ans #1 :

Why does the ImageButton make an image as big as this so small, when it could fill most of the screen, even with the ImageButton size being very wide?

The answer is is similar to the above mentioned. Your Image size will be much lesser than that the Android OS calculated depending on your mentioned size, taking device resolution into consideration. Use the http://coh.io/adpi/ (or http://density.brdrck.me/) site to make it simple to calculate. Always take the mdpi as standard or base. Suppose your image size is 988X89 in mdpi, it will be:

enter image description here Now, it is as if you are trying to view an image of 988px X 89px on an xhdpi screen where the Android Os calculated the image size as 1976px X 178px, which is way larger than your given image size of 988px X 89px. that's why you get small image with most area white.

How to correct it?
Go through the Android specifications to support different densities screens. this requires you to design images specifically for hdpi, mdpi, xhdpi and put them in appropriate folders of:

 MyProject/
res/
drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
awesomeimage.png

Ref : http://developer.android.com/training/multiscreen/screendensities.html for more details

Adjusting the size of an ImageButton in Android

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

How to resize imagebutton in android xamarin?

You should try to use scaleType.

This example might be very helpful

And the solution should be android:scaleType="fitXY".

EDIT

Because question was edited and it occurs that it is ImageButton, the solution for that is different scaleType and one more option.

android:scaleType="fitCenter"
android:adjustViewBounds="true"

Also padding might be too big.



Related Topics



Leave a reply



Submit