Android XML layer-list: How to position the top layer

16,780

Use an inner Bitmap drawable inside the item, and set gravity for it:

<item android:drawable="@drawable/tab_background_unselected">
</item>
<item>
    <bitmap
        android:gravity="bottom|center_horizontal"
        android:src="@drawable/tab_selected_arrow" />
</item>
Share:
16,780
Asaf Nevo
Author by

Asaf Nevo

Current CEO @Pico - Converting Events into Engagement and the company's former CTO. Experience in developing variety of different environment including DB, server side and client side. Done SQL, C#, PHP, Java, Android, IOS, Web.. Love the StackOverflow community and use it on a daily basis

Updated on June 18, 2022

Comments

  • Asaf Nevo
    Asaf Nevo almost 2 years

    I have this XML drawable - tab_background_unselected:

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

    which creates this shape:

    background rectangle

    and this arrow shape xml drawable - tab_selected_arrow:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <rotate
                android:fromDegrees="45"
                android:pivotX="-40%"
                android:pivotY="87%"
                android:toDegrees="45" >
                <shape android:shape="rectangle" >
                    <solid android:color="@color/background_dark_green" />
                </shape>
            </rotate>
        </item>
    
    </layer-list>
    

    which creates this shape:

    top layer triangle

    I'm using this drawable XML (instead of PNG file) in order to create a layer-list:

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

    but I want the final image to look like this:

    final image

    I don't know how to set the gravity of the arrow (the second item and the top layer) to center|bottom... I've tried using bitmap tag but it only accepts image files.

    I need this to be a XML drawable because

    1. I need it to be inside a drawable selector
    2. I don't want to make it a PNG and create different file for each screen resolution
  • Jose_GD
    Jose_GD about 10 years
    Asaf said he tried with Bitmap drawables, and he's correct: they don't allow XML drawables (I've just corrected the question and now the "bitmap" tag in "I've tried using..." appears)
  • djdance
    djdance almost 8 years
    AFAIK no way, there is a bug in API 21-22, no vectros in bitmap element here :(
  • superuser
    superuser almost 7 years
    Should be more highlighted: this answer only works when "tab_selected_arrow" is a .png!!!