mvand
mvand

Reputation: 1183

Android: gravity attribute on item drawable

I want to achieve this kind of style

image1

As you can see, my background app is a texture so i need a transparent background and just a few pixels of gradient color at the bottom of the view.

I used simply this to describe my background of EditText

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">>
     <item
         android:gravity="bottom"
         android:height="2dp"
         android:drawable="@drawable/my_gradient">
     </item> 
</layer-list>

It works well on Android 6.0 and Android M Preview but not on "older version" as Android 5.1.1

API 23 and 22(M) versus API 22 and older rendering image2

I can't understand why my gravity attribute is not applied

Upvotes: 2

Views: 1980

Answers (1)

Philippe Banwarth
Philippe Banwarth

Reputation: 17755

It seems that the gravity (and size) property for LayerDrawable children are recent, LayerDrawable.setLayerGravity() indicates Added in API level 23.

Edit : As for a solution, here is what I could think of:

  • Use a nine patch as background, with a transparent vertically extensible part

  • Overlay the unwanted part of the gradient with another shape, having the same color as the background (wich obviously needs to be opaque)

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/my_gradient" />
    
        <item android:bottom="2dp">
            <shape>
                <solid android:color="background color" />
            </shape>
        </item>
    </layer-list>
    
  • Use a View to hold the gradient. Depending on how the EditText, is used in the layout it may be necessary to add also a Framelayout, for example :

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_gravity="bottom"
            android:background="@drawable/my_gradient" />
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text" />
    </FrameLayout>
    

Upvotes: 2

Related Questions