Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

Adding margins to listview

I am following a tutorial and I did not understand why exactly the margins that I applied to a list view contained within a relative layout does not affect the look when I test it on a device. However, if I put everything in a LinearLayout container it looks as it should. I don't understand why the layout does not work without this LinearLayout.

This is my code in layout for my listView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
          android:background="@android:color/transparent">
<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/inbox_horizontal_margin"
    android:layout_marginRight="@dimen/inbox_horizontal_margin"
    android:layout_marginTop="@dimen/inbox_vertical_margin"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageIcon"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
        android:paddingBottom="@dimen/message_list_item_vertical_margin"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/content_desc_message_icon"
        android:src="@drawable/ic_action_picture"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/senderLabel"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_toRightOf="@+id/messageIcon"
        android:layout_toEndOf="@+id/messageIcon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=" created at: 03.17.15"
        android:id="@+id/createdAtLabel"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/senderLabel"
        android:layout_toEndOf="@+id/senderLabel"/>
</RelativeLayout>
</LinearLayout>

This is the result of the code: enter image description here

this is the code without the LinearLayout:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/inbox_horizontal_margin"
    android:layout_marginRight="@dimen/inbox_horizontal_margin"
    android:layout_marginTop="@dimen/inbox_vertical_margin"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageIcon"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
        android:paddingBottom="@dimen/message_list_item_vertical_margin"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/content_desc_message_icon"
        android:src="@drawable/ic_action_picture"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/senderLabel"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_toRightOf="@+id/messageIcon"
        android:layout_toEndOf="@+id/messageIcon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=" created at: 03.17.15"
        android:id="@+id/createdAtLabel"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/senderLabel"
        android:layout_toEndOf="@+id/senderLabel"/>
</RelativeLayout>

And this is the result of this layout:

enter image description here

Upvotes: 1

Views: 58

Answers (1)

rahul.ramanujam
rahul.ramanujam

Reputation: 5618

Margin is outside a view , ListView uses AbsListView.LayoutParams by default, which doesn't include any margin support, just the height and width, thats why, it simply ignores the params value for margins. Try using padding for your relative layout instead

Upvotes: 1

Related Questions