Search code examples
androidandroid-layoutandroid-listview

Android: margin/padding for the ListView without having the margin applied to the header?


Is it possible to have a margin/padding for the ListView without having the margin applied to the header? I want my ListView to be like the Google Now layout. I've got everything working except for this little problem.

Question: Is it possible to not have layout parameters of the ListView apply to its header?

EDIT1:More info

The purple view is the header to the listView. I have tried adding margin to the list item layout.This doesnt work either. All i need is a way to apply margin to the listview so that it doesnt apply the margin to the header in the listView.

EDIT2: My layouts header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/top_height"
        android:background="@color/top_item" />

    <View
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sticky_height"
        android:layout_gravity="bottom" />

</FrameLayout>

root_list.xml

<LinearLayout>
<com.test.CustomListView
    android:id="@android:id/list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_height="wrap_content" /></LinearLayout>

list_item.xml

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

<TextView
    android:id="@+id/textview_note"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview_note_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/></LinearLayout>

I've removed uselsess layout properties here due to length of this post. This is how it looks


Solution

  • After much digging around i found that its is not posssible to decouple the listView layout params from its headers and the header is part of listView. So in order to get the desirred margin effect, After much looking around I found out that adding margin to your root of the layout in the list view item didnt work. So I added another dummy LinearLayout inside the existing root LinearLayout(a nested LinearLayout) and added the children inside the nested layout.Now setting margin to the inner layout gives the desired effect.

    Code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginLeft="16dp"
            android:background="@drawable/list_selector"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/textview_note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/textview_note_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
    </LinearLayout>