Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Customizing a ListView for the Inbox Adding Margins to ListViews

In this challenge we are going to add some margins to a ListView. Start by declaring a new dimension in dimens.xml belo

How do i do this.

dimens.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">

</resources>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/item_background_color" >

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/content_desc_item"
            android:src="@drawable/ic_item" />

    </RelativeLayout>

</LinearLayout>

4 Answers

<resources xmlns:android="http://schemas.android.com/apk/res/android"> <dimen name="horizontal_margin">10dp</dimen> <dimen name="vertical_margin">6dp</dimen>

</resources>

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

For this example you need to create a field in the dimens.xml.

Say I did something like this in my dimens.xml

' ' ' dimen name="activity_horizontal_margin">16dp</dimen> ' ' '

Now, this works a lot like string.xml, styles.xml etc

Now, in my layout. I would specify something like this. Say my app has 50 layouts. Each layout has the same picture at the top. This picture needs to be 16dp as the horizontal margins.

In my activities xml. I can do this.

' ' 'html <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" <--------- NOTICE HERE android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sk8.skateboardsetup.AltboardsetupOne" > ' ' '

Now everyone of my layouts will have this value. I specify it that it's in the dimens with @dimens/<name> In this instance I gave it actiivty_hoizontal_margine.

This is great because like I said, I have 50 different layouts. So instead of going to each and every layout and saying change dp to 16. I can just go to the dimens.xml and change it from 16 to 20 or 15 whatever I want. If I edit that one file, every single layout within my application that has the property set as the padding. Will pick up my change.

Create the dimen tag, set it to a name and give it a value and close the tag. Then in your layout use adroid:<whatever you are setting just so as long as it takes a digit> you can input the dimen reference.

Does this help you? Let me know if you have any other questions I can help clarify.

<dimen name="horizontal_margin">10dp</dimen>

<resources xmlns:android="http://schemas.android.com/apk/res/android"> <dimen name="horizontal_margin">10dp</dimen> <dimen name="vertical_margin">6dp</dimen>

</resources>