Calling setCompoundDrawablesWithIntrinsicBounds on multiple views with the same background gives inconsistent sizes

I have LinearLayoutwith two descendants EditText(not direct children). I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0)on each to place at one end (the same drawable) on the right side of each of EditTexts. The two editing texts are identical except for their order in ListView. However, the size of the extracted in the first EditTextbecomes larger than the second after adding the drawings. In particular, the first EditTextresizes to fit the internal size of the stretch to be drawn, while the second EditTextdoes not (although it grows slightly).

Why do two identical drawings look different in two almost identical representations?

Also, I could solve this problem by figuring out how to make drawable not by force EditText to resize. As you can see, I followed the directions of https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList regarding creating a background scale, but that doesn't seem to do the trick.

the code:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView 
        style="@style/fieldLabel"
        android:text="phone number"/>
    <EditText
        android:id="@+id/contactPhoneField" 
        android:inputType="phone"
        android:layout_width="fill_parent"
        style="@style/editableField"/>

</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView 
        style="@style/fieldLabel"
        android:text="e-mail address"/>
    <EditText
        android:id="@+id/contactEmailField" 
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"
        style="@style/editableField"/>

</LinearLayout>

recoverable:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>

        <shape android:shape="rectangle">

            <gradient 
                android:startColor="#FFD060" 
                android:endColor="#FFBB33"
                android:angle="270"/>
            <padding android:left="5dp" android:top="5dp"
                android:right="5dp" android:bottom="5dp" />
            <corners android:radius="5dp" />
            <stroke android:width="2dp" android:color="#FF8800"/>
        </shape>

    </item>
    <item android:drawable="@drawable/warn"/><!-- a 35x35 px png which is taller than the native height of an EditText-->
</layer-list>

then at some point I call:

contactPhoneField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
contactEmailField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);

In addition, each subsequent time that I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0)(after its first call) causes the first size EditTextto switch between a 2nd EditTextmatch and a valid size match!

+3
source share

All Articles