android – How to specify spacing between elements of LinearLayout only once?-ThrowExceptions

Exception or error:

I recently ran into a problem again that I already had several times in the last years.

LinearLayout is a very convenient layout manager. But what I totally miss is the possibility to add a certain space between the elements (like padding) in a single XML tag.

What I mean by one tag is, that I can define in the declaration of the LinearLayout the spacing between the elements (e.g. in a vertical LinearLayout the vertical space between two elements in this layout).

I know that I can do it by adding the XML tag android:layout_marginTop or something similar to every element in the LinearLayout.

But I would like to be able to define it in only one point, as the spacing is the same for all elements.

Does anybody know an easy way to do this (not implementing a custom LinearLayout or something like that)? I prefer a solution that works directly in XML without the need for coding.

How to solve:

the way that is recommended is to apply a style to all the elements in the linear layout

android:style="@style/mystyle"

<style name="mystyle">
      <item name="android:layout_marginTop">10dp</item>
      ... other things that your elements have in common
</style>

###

Set custom transparent drawable as a divider for your layout:

<LinearLayout
  android:showDividers="middle"
  android:divider="@drawable/divider">

New drawable resource in drawables folder (divider.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
  <size
    android:width = "0dp"
    android:height = "16dp"/>
</shape>

###

@Chris-Tulip’s answer really helped me – with good practice too.

For those of you who might be getting an Eclipse error about missing a resource identifier for “Style” in package android, like I did, you don’t need to add the android namespace.

So,
android:style=”xx” brings up the error, while style=”xx” is correct. Funky, but for anyone having that error, this might help.

###

You should add android:layout_marginTop or android:layout_marginLeft to element which must have indent. Depends on android:orientation of your LinearLayout.

###

You could define your single item “prototype” in a separate xml file and then inflate the items from that file, dynamically in code and insert them into your linear layout.

You would then define the spacing on the actual item, not the parent LinearLayout, (as a android:layout_marginTop for example) and that spacing would be applied to all your items as you inflate them.

EDIT:

container.xml:

<LinearLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your items will be added here -->

</LinearLayout>

item.xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is my child" />

</LinearLayout>

MyActivity.java:

// Put this in a suitable place in your Java code, perhaps
// in "onCreate" or "onResume" depending on where and how
// you initialize your view. You can, of course inflate
// any number of instances of the item and add them to
// your parent LinearLayout.
LayoutInflater inflater = LayoutInflater.from(context);
View item = inflater.inflate(R.layout.item, null, false);

LinearLayout container = findViewById(R.id.parent);
container.addView(view);

I haven’t put an effort in testing the code, but it “should” work as is 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *