Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fit png background image to the size of imagebutton in android sdk

Tags:

android

i have 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>

its working fine but my problem is that my button width is 50 and my image png width is 70. how can i fit this png to get resized to my button width automatically. Right now no matter what width i give to my button its width gets to the size of image png width.

can someone help me how to do this.

Thanks

like image 760
Rishi Avatar asked Dec 22 '22 13:12

Rishi


2 Answers

try adding to xml button:

                android:scaleType="fitCenter"
like image 189
Buda Gavril Avatar answered May 14 '23 12:05

Buda Gavril


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

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

Update: It might also be a good idea to scale down your images to fit the 50dip width, and use 9-patch for auto-scaling (when orientation changes for example).

like image 38
dimi Avatar answered May 14 '23 12:05

dimi