DaveSav
DaveSav

Reputation: 1374

Button resizes on > 2.3.6

This is what is happening:

button resizes horizontally in gt 2.3.6

This is the source image:

source image

This is part of the xml layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_headers"
    android:orientation="horizontal" >

    <AutoCompleteTextView
        android:id="@+id/etNewItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:completionThreshold="2"
        android:dropDownHeight="wrap_content"
        android:dropDownWidth="wrap_content"
        android:hint="@string/et_newitemhint"
        android:inputType="text|textCapSentences"
        android:layout_centerVertical="true"
        style="@style/DialogEditText"
        android:layout_marginRight="10dp"
        android:singleLine="true" />


    <Button
        android:id="@+id/btnAddNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_add"
        android:layout_centerVertical="true" />
</RelativeLayout>

This is the Selector xml for the button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" 
    android:drawable="@drawable/btn_add_pressed"/>
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_add_pressed"/>
<item android:drawable="@drawable/btn_add_normal"/>
</selector>

Why is this button horizontally resizing in 4.x.x? The source graphic did use to be a 9-patch, but it did exactly the same thing, so I changed it to a normal png, with no result.

The odd thing is is that this is the only graphic which exhibits this behaviour. I have it in an actionbar somewhere: same thing; I have it in a dialog too: same thing.

My 2.2.2 and 2.3.6 test devices show the button as it should be, but any of my test devices of 4.x.x show the elongated button.

Upvotes: 1

Views: 314

Answers (2)

Malcolm
Malcolm

Reputation: 41510

I didn't manage to reproduce this on my devices. However, I still have a solution: you can use exact sizes like this:

android:layout_width="50dp"
android:layout_height="50dp"

This way the image should look the same on every device.

Upvotes: 0

Ivan Skoric
Ivan Skoric

Reputation: 1210

First, make these:

res/drawable/button_add_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/btn_add_normal"
    android:tileMode="disabled" 
    android:gravity="top" >
</bitmap>

res/drawable/button_add_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/btn_add_pressed"
    android:tileMode="disabled" 
    android:gravity="top" >
</bitmap>

Then use this as selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_focused="true" 
           android:drawable="@drawable/button_add_pressed"/>
     <item android:state_pressed="true" 
           android:drawable="@drawable/button_add_pressed"/>
     <item android:drawable="@drawable/button_add_normal"/>
</selector>

Upvotes: 1

Related Questions