Search code examples
androidlistviewdrawable

Custom listview selector / drawable dimensions?


Is there any way I can set my listview selector to a custom drawable that only occupies a portion of the item view? I have made a drawable rectangle and set it as the selector, but I'm having a difficult time getting it to size properly. The picture shows what I have now (1) and the desired effect (2).


Solution

  • Got it - initially decided to just set the selector as a drawable that only shows a border on one side, and messed around with insets to no avail (for some reason the top and bottom were not working properly). So I just made a layer list and offset the top layer to the right by a few dp.

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/colorAccent"/>
            </shape>
        </item>
        <item android:top="0dp" android:left="5dp" android:bottom="0dp" android:right="0dp">
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF"/>
            </shape>
        </item>
    </layer-list>