ab11
ab11

Reputation: 20090

How to make space between LinearLayout children?

I am programatically adding custom views to a vertical LinearLayout, and I would like there to be some space between the views. I have tried adding: setPadding(0, 1, 0, 1) to my CustomView constructor, but this doesn't seem to have any effect. Any advice?

*It was pointed out that I should use margins. Since I am dynamically adding views, I need to set the margins from code (not in xml). I believe the way to do this is below, but it isn't working.

public class MyView extends View
{
    public MyView (Context context)
    {
        super(context);

        MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 10, 0, 10);
        setLayoutParams(params);

*Edit. I also tried using MarginLayoutParams as a parameter while adding the views to the Linear layout (as below). This also did not work:

MarginLayoutParams params = new MarginLayoutParams(linearLayout.getLayoutParams());
linearLayout.setMargins(0, 10, 0, 10);
linearLayout.addView(view, params);

Upvotes: 222

Views: 367019

Answers (13)

ProgrammerJibon
ProgrammerJibon

Reputation: 71

This can solve problem,

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

Past this between your child view. if you add a blank view with layout weight, this will make a space between two view like flex space between in html.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btnNeutral"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Negative"
        android:textColor="@android:color/white"/>
    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="0dp"/>
    <Button
        android:id="@+id/btnNegative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Negative"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btnPositive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Positive"
        android:textColor="@android:color/white" />
</LinearLayout>

Upvotes: 1

Rufai DEMIR
Rufai DEMIR

Reputation: 135

You can use android:layout_weight="1"

Like This: enter image description here

Upvotes: 3

Sinan Aktepe
Sinan Aktepe

Reputation: 180

You just need to wrap items with linear layouts which have layout_weight. To have items horizontally separated, use this

<LinearLayout
    ...
    ...
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center">

      // your item

  </LinearLayout>
</LinearLayout>

Upvotes: 8

Oleg Nekrasov
Oleg Nekrasov

Reputation: 61

Try to add Space widget after adding view like this:

layout.addView(view)
val space = Space(context)
space.minimumHeight = spaceInterval
layout.addView(space)

Upvotes: 5

teh.fonsi
teh.fonsi

Reputation: 3134

Since API Level 14 you can just add a (transparent) divider drawable:

android:divider="@drawable/divider"
android:showDividers="middle"

and it will handle the rest for you!

Upvotes: 11

Harish Rana
Harish Rana

Reputation: 484

You can get the LayoutParams of parent LinearLayout and apply to the individual views this way:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(8,8,8,8);
  • Take care that setMargins() take pixels as int data type.So, convert to dp before adding values
  • Above code will set height and width to wrap_content. you can customise it.

Upvotes: 1

Francis Bacon
Francis Bacon

Reputation: 4745

Using padding in the layout of Child View.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="5dp"
          android:background="@drawable/backage_text"
          android:textColor="#999999"
           >

</TextView>

backage_text.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/white"/>
    <corners android:radius="2dp"/>
    <stroke
        android:width="1dp"
        android:color="#999999"/>
    <padding
        android:bottom="5dp"
        android:left="10dp"
        android:right="10dp"
        android:top="5dp" />
</shape>

Upvotes: 4

worked
worked

Reputation: 5880

Android now supports adding a Space view between views. It's available from 4.0 ICS onwards.

Upvotes: 43

android developer
android developer

Reputation: 116332

If you use ActionBarSherlock, you can use com.actionbarsherlock.internal.widget.IcsLinearLayout :

<com.actionbarsherlock.internal.widget.IcsLinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@drawable/list_view_divider"
        android:dividerPadding="2dp"
        android:showDividers="middle" >
...
</com.actionbarsherlock.internal.widget.IcsLinearLayout>

Upvotes: 3

riwnodennyk
riwnodennyk

Reputation: 8258

The API >= 11 solution:

You can integrate the padding into divider. In case you were using none, just create a tall empty drawable and set it as LinearLayout's divider:

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

empty_tall_divider.xml:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size
            android:height="40dp"
            android:width="0dp"/>
</shape>

Upvotes: 262

Gorky
Gorky

Reputation: 1413

The sample below just does what you need programatically. I have used a fixed size of (140,398).

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
        layoutParams.setMargins(24, 0, 24, 0);
        layout.addView(button,layoutParams);

Upvotes: 26

Chris Fei
Chris Fei

Reputation: 1317

Use LinearLayout.LayoutParams instead of MarginLayoutParams. Here's the documentation.

Upvotes: 3

Thomas
Thomas

Reputation: 3693

You should android:layout_margin<Side> on the children. Padding is internal.

Upvotes: 174

Related Questions