Search code examples
androidandroid-listview

Custom listview with shodows to the listview row in android


I know how to make a custom listview with Layout inflator and use of the xml layout for row of listview. But I want to know how different styles we can apply on the listview row like I shown one below.

Listview with shadows in rows


Solution

  • Here is listview_item_shadow.xml:

    <?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="@android:color/darker_gray" />
            </shape>
        </item>
         <item 
             android:right="1dp" 
             android:bottom="2dp">        
            <shape android:shape="rectangle">
                  <solid android:color="@android:color/white"/>
            </shape>
         </item>         
    </layer-list>
    

    Now you should use it in selector for ItemView! - listview_item_backgroundstate.xml You need to set listview_item_backgroundstate.xml as background to your ListView item

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

    And at last you have to set custom_selector.xml as in ListView. android:listSelector="@drawable/custom_selector.xml"

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

    Or use that kind of image as background for every list item..