views:

769

answers:

3

Can anybody tell me how to resize the imageButton to fit the image exactly..This is the code that i tried, but the image is placed at the position that i am locating using android:scaleType but i can't able to reduce the size of imageButton, pls help me out in rectifying this issue...The code that i tried is ...

<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>
+1  A: 

You're probably going to have to resize the button programmatically. You'll need to explicitly load the image in your onCreate() method, and resize the button there:

public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  ImageButton myButton = (ImageButton) findViewById(R.id.button);  
  Bitmap image = BitmapFactory.decodeResource(R.drawable.eye);  
  myButton.setBitmap(image);  
  myButton.setMinimumWidth(image.getWidth());  
  myButton.setMinimumHeight(image.getHeight());
  ...
}

It's not guaranteed to work, according to the specifications for setMinimumX (since the width and height are still dependent on the parent view), but it should work pretty well for almost every situation.

infinitypanda
A: 

hi have you tried?

android:background="@drawable/eye"
Jorgesys
+1  A: 

Kinda late, but im all for 'better late then never' I had the same question today, found it explained here: http://www.anddev.org/tutorial_buttons_with_niceley_stretched_background-t4369.html

Olivier Henny