BT643
BT643

Reputation: 3845

Android Card UI Style ListView with Contextual Action Bar?

I'm just starting with Android development and trying to get a "Cards UI" style ListView which also uses the Contextual Action Bar to highlight multiple items for deletion, etc. I just want the background colour to change to blue when the item is selected.

I managed to get the LOOK I wanted using a layer-list, but I couldn't get the background colour to change when selected etc in an elegant way. From my understanding, I need to use a "selector" drawable for that? But when I use that, I no longer know how to get the LOOK of the cards UI.

Here's how I want it to look, and be able to change the background to blue when selected.

With layer-list

Here's what I've done with the selector drawable, which functionality-wise, in terms of being able to change the background colour when selected, it's fine, but as you can see it looks wrong. I just need the grey at the bottom like the above image.

Using Selector Looks Wrong

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:background="@drawable/card_background">

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/horse_image"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/horse_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left|center_vertical"
        android:textSize="20dip"
        android:layout_marginLeft="10dip"
        android:text="@string/loading_horses" />

</LinearLayout>
</FrameLayout>

card_background.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:bottom="2dp">
    <shape
        android:shape="rectangle"
        android:dither="true">
        <stroke android:width="5dp" android:color="#FFDDDDDD" />
        <corners android:radius="2dp" />
        <solid android:color="@android:color/white" />

        <padding
            android:bottom="8dp"
            android:left="8dp"
            android:right="8dp"
            android:top="8dp" />
    </shape>


</item>

<item android:state_activated="true" android:bottom="2dp">
    <shape
        android:shape="rectangle"
        android:dither="true">
        <stroke android:width="5dp" android:color="#FFDDDDDD" />
        <corners android:radius="2dp" />
        <solid android:color="@android:color/holo_blue_light" />

        <padding
            android:bottom="8dp"
            android:left="8dp"
            android:right="8dp"
            android:top="8dp" />


    </shape>
</item>

<item android:bottom="2dp">
    <shape
        android:shape="rectangle"
        android:dither="true">
        <stroke android:width="5dp" android:color="#FFDDDDDD" />
        <corners android:radius="2dp" />
        <solid android:color="@android:color/white" />

        <padding
            android:bottom="8dp"
            android:left="8dp"
            android:right="8dp"
            android:top="8dp" />
    </shape>
</item>


</selector>

Upvotes: 3

Views: 5695

Answers (2)

MrLithid
MrLithid

Reputation: 281

background_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"
            android:dither="true">
            <corners android:radius="2dp"/>
            <solid android:color="#CCC" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:shape="rectangle"
            android:dither="true">
            <corners android:radius="2dp" />
            <solid android:color="#22000000" />
            <padding android:bottom="6dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>
</layer-list>

background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"
            android:dither="true">
            <corners android:radius="2dp"/>
            <solid android:color="#CCC" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:shape="rectangle"
            android:dither="true">
            <corners android:radius="2dp" />
            <solid android:color="#FFF" />
            <padding android:bottom="6dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>
</layer-list>

list_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/background_pressed" android:state_activated="true"/>
    <item android:drawable="@drawable/background_pressed" android:state_checked="true"/>
    <item android:drawable="@drawable/background_pressed" android:state_selected="true"/>
    <item android:drawable="@drawable/background_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/background_pressed" android:state_active="true"/>
    <item android:drawable="@drawable/background"/>
</selector>

Give your list items some padding and apply the list_item_background as the list items background.

Upvotes: 10

k3v1n4ud3
k3v1n4ud3

Reputation: 2994

I think you might want to try android:state_selected to cover the selection case.

Upvotes: 0

Related Questions