Pavan Kumar
Pavan Kumar

Reputation: 1686

Image is stretching in imagebutton as per width and height

This ImageButton is clickable. I need to present this icon in center without stretch. Is there any way to add width and height or property? Please give me a idea. Thanks

<ImageButton
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:background="@drawable/usercalltab"/>

usercalltab.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                <shape android:shape="rectangle" >
                    <solid android:color="#ffdbab" />
                </shape>
            </item>
            <item>
                 <bitmap android:src="@drawable/search"/>
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                 <bitmap android:src="@drawable/callme"/>
            </item>
        </layer-list>
        </item>
</selector>

enter image description here

Upvotes: 2

Views: 2961

Answers (2)

tnj
tnj

Reputation: 913

I think you should split it into background and foreground image. It's much cleaner and re-usable.

If you want to achieve it without doing so, you may try this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="#ffdbab" />
                </shape>
            </item>
            <item>
                 <bitmap android:src="@drawable/search" android:gravity="center" />
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/transparent" />
                </shape>
            </item>
            <item>
                 <bitmap android:src="@drawable/callme" android:gravity="center" />
            </item>
        </layer-list>
    </item>
</selector>

Upvotes: 3

FoamyGuy
FoamyGuy

Reputation: 46856

try like this:

<ImageButton
android:layout_width="60dp"
android:layout_height="wrap_content"
android:src="@drawable/usercalltab"
android:scaleType="centerInside"
/>

or maybe android:scaleType="center"

one of those should be what you are looking for I think

Upvotes: 4

Related Questions