Search code examples
androidandroid-layoutlistviewnine-patch

android: listview and linearlayout padded from the edges of parent layout?


enter image description here

So, I want to create a layout with listview and Linearlayout clearly specified with the image.

The padding i.e. required from all the edges of the device is important for me and this should work in a responsive manner for different devices it should be padded from various sides.

Could I Use Nine patch in this case , if yes how?

How could I achieve this , any skeleton layout will solve my problem.


Solution

  • To get that spacing you should use margin, not padding.

    Here's a solution:

    <RelativeLayout        
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/mylistview"
            android:layout_width="match_parent"
            android:layout_height="@dimen/listview_height"
            android:layout_margin="@dimen/standard_margin"
            android:layout_alignParentTop="true"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/mylistview"
            android:layout_margin="@dimen/standard_margin>
    
        </LinearLayout>
    
    
    </RelativeLayout>
    

    This sets a fixed value for the ListView's height, and makes the LinearLayout automatically resize to fit the remaining space (because of layout_below).

    Then create a dimensions file in res/values:

    E.g.

    dimens.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <dimen name="listview_height">200dp</dimen>
        <dimen name="standard_margin">30dp</dimen>
    
    </resource>
    

    To support different screen sizes you can have different dimensions in other folders such as /res/values-large, /res/values-xlarge etc.