Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I get a ListView Item to be elevated?

I am trying to get it so my listview items are elevated and show a drop shadow.

Here is my list view item layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:translationZ="5dp"
android:cropToPadding="false">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView4"
    android:background="@color/background_white"
    android:layout_alignBottom="@+id/PostUpvote"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:translationZ="2dp"

     />

and here is my ListView layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal"
    android:background="@color/background_white"
    android:dividerHeight="10sp"
    android:translationZ="2dp"/>

Any help would be appreciated.

like image 953
Errol Green Avatar asked Apr 19 '15 04:04

Errol Green


People also ask

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });

What is an Expandable List View?

Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.

What is a custom list view?

Android Custom ListView (Adding Images, sub-title) After creating simple ListView, android also provides facilities to customize our ListView. As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc).


1 Answers

If you are targeting version 21 you can use

android:elevation="10dp"

If not then you may need to create the drop shadow effect yourself in a drawable. For example this one creates drop shadow on right and bottom sides.

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

 <!-- Adding border layers simulating drop shadow -->
 <item>
    <shape>

        <padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
        <solid android:color="#00494949" />
    </shape>
</item>

 <item>
    <shape>
        <padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
        <solid android:color="#10494949" />
    </shape>
</item>

 <item>
    <shape>
        <padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
        <solid android:color="#20494949" />
    </shape>
</item>

 <item>
    <shape>
        <padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
        <solid android:color="#30494949" />
    </shape>
</item>

<item>
    <shape>
      <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
        <solid android:color="#70494949" />
    </shape>
</item>

<item>
<shape>
        <solid android:color="@android:color/white" />
</shape>
</item>

Save it in drawable as my_drop_shadow.xml and use it in any View with android:background="@drawable/my_drop_shadow"

You can adjust shadow direction by changing padding values and add elevation effect by controlling transparency.

like image 132
inmyth Avatar answered Oct 16 '22 18:10

inmyth