24

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.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Dude
  • 652
  • 2
  • 10
  • 24

6 Answers6

20

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>
chris-tulip
  • 1,840
  • 1
  • 15
  • 22
  • Thanks @MrFox. That's the kind of solution I was looking for. – Dude Sep 27 '12 at 15:28
  • 2
    I'm having the same question as the OP. However, I'm not sure how this approach accomplishes what I'm looking for. For example, if I have 10 textviews inside my LinearLayout and I want a spacing of 10dp between all of them, I'd like to be able to specify in one place that 10dp setting. If I define a style as in your example, I still have to apply this style to all 10 textviews. Am I missing something here? – AngieM Oct 06 '14 at 19:50
  • 1
    @AngieM use this [SO Answer](http://stackoverflow.com/a/18538656/923340) it proposes to use dividers of LinearLayout to apply even spacing between LL children. – Lukasz 'Severiaan' Grela Oct 14 '14 at 09:00
  • Don't fool yourself - you only spacing particular elements inside LinearLayout. If I, for example, need to hide/show some of them dynamically - the spacing will be broken (e.g. I might end up showing spacing of LinearLayout's top margin + 2nd elements divider spacing (which is redundant) if I make 1st element GONE). Very unreliable solution, IMO – Den Drobiazko Jan 19 '16 at 17:06
  • 1
    Fair enough, was the best solution at the time back in 2012 when I posted :) – chris-tulip Feb 12 '16 at 19:58
19

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>
Zon
  • 18,610
  • 7
  • 91
  • 99
1

@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.

Watercayman
  • 7,970
  • 10
  • 31
  • 49
0

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 :-)

dbm
  • 10,376
  • 6
  • 44
  • 56
  • Thanks dbm. Thanks for the nice code example. I would prefer a solution that works directly in XML, but if no better solution comes up I'll pick yours as answer. – Dude Sep 17 '12 at 08:15
  • I understand. Unfortunately I don't know of a pure XML-only solution. – dbm Sep 17 '12 at 09:13
  • 1
    Never use 5dp in Android. Always use 4, 8, 16, 32, 48 dp. http://developer.android.com/design/style/metrics-grids.html – Fred Jun 23 '14 at 23:14
0

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

Yuriy Vasylenko
  • 3,031
  • 25
  • 25
0

For vertical space

<View
    android:layout_width="0dp"
    android:layout_height="20dp"/>

For horizontal space

<View
    android:layout_width="20dp"
    android:layout_height="0dp"/>
Ilyas Arafath
  • 511
  • 7
  • 13