How to specify spacing between elements of LinearLayout only once?
Asked Answered
M

6

25

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.

Multitudinous answered 17/9, 2012 at 7:38 Comment(2)
For any other searching for a single place solution.Kythera
Here's an excellent blog post about it: Grid Spacing on Android by Cyril MottierSedimentology
R
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>
Rabbitry answered 18/9, 2012 at 0:7 Comment(5)
Thanks @MrFox. That's the kind of solution I was looking for.Multitudinous
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?Anything
@Anything use this SO Answer it proposes to use dividers of LinearLayout to apply even spacing between LL children.Kythera
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, IMOLaudation
Fair enough, was the best solution at the time back in 2012 when I posted :)Rabbitry
D
20

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>
Devour answered 26/7, 2017 at 13:17 Comment(1)
Man, this asnwer is so much better than the accepted oneThunder
D
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.

Duplicity answered 10/6, 2014 at 3:48 Comment(0)
N
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 :-)

Novia answered 17/9, 2012 at 7:49 Comment(3)
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.Multitudinous
I understand. Unfortunately I don't know of a pure XML-only solution.Novia
Never use 5dp in Android. Always use 4, 8, 16, 32, 48 dp. developer.android.com/design/style/metrics-grids.htmlGrazia
R
0

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

Route answered 23/3, 2015 at 9:5 Comment(0)
J
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"/>
Josiah answered 20/9, 2022 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.