2

I am trying to resize(shrink) imagebutton in .xaml file in xamarin android, but i am getting imagbutton being cut in both top and bottom.Can anyone suggest me how could i shrink image to required size without any cut in image.

i am using visual studio 2015 tool to build android in xamarin

below is my code

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myButton"
    android:layout_width="120dip"
    android:layout_height="150dip"
    android:layout_marginTop="50dip"
    android:layout_marginLeft="50dip"
    android:padding="100dip"
    android:src="@drawable/geocursor_pressed" />

EDITED ANSWER:

these extra 3 things should be added

  android:adjustViewBounds="true"
    android:padding="10dp"
    android:scaleType="fitCenter"

 By changing padding you can get desired fit of image in button
 Refer Link for more information :    

Fit Image in ImageButton in Android

Community
  • 1
  • 1
dafodil
  • 531
  • 15
  • 30

1 Answers1

3

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.

  • if i use scaletype image is getting cutoff,but i need it to scale it to particular size without any cuting. – dafodil Mar 03 '16 at 07:05
  • Try to add also android:background="@null". fitXY should resize your image to fit in your width and height. And notice that padding might give you different view than you are expecting. –  Mar 03 '16 at 07:08
  • But still it doesnt work with my hints ? Could you show screenshot ? –  Mar 03 '16 at 10:08
  • its working, just i have to give padding.Please edit answer to give padding – dafodil Mar 03 '16 at 10:55