How to put png background image on image button size in android sdk

I created an image button in my xml

<Button android:layout_height="wrap_content" android:id="@+id/send" android:layout_width="50dip" android:background="@drawable/button_back"> </Button> 

and its background, like this "button_back.xml"

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/back_pressed" /> <!-- pressed png --> <item android:state_focused="true" android:drawable="@drawable/back_focused" /> <!-- focused png --> <item android:drawable="@drawable/back_normal" /> <!-- default png --> </selector> 

works fine, but my problem is that the width of my button is 50 and the width of the image is 70. how can I fit this png to automatically resize my button. Right now, no matter what width I attach to my button, its width reaches the size of the width of a png image.

can someone help me how to do this.

thanks

+4
source share
3 answers

I believe the 9 patch is what you are looking for.

Looks here: http://developer.android.com/guide/developing/tools/draw9patch.html and find the "Nine-patch" here: http://developer.android.com/guide/topics/graphics/2d-graphics .html # nine-patch

Update. It might also be a good idea to reduce your images to fit the width of 50dip, and use the 9 patch to automatically scale (for example, when changing orientation).

+2
source

try adding to the xml button:

  android:scaleType="fitCenter" 
+8
source

All Articles