How to make space between LinearLayout children?

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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!

Solution 6 - Android

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

Solution 7 - Android

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>

Solution 8 - Android

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>

Solution 9 - Android

Try to add Space widget after adding view like this:

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

Solution 10 - Android

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>

Solution 11 - Android

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.

Solution 12 - Android

An easy way to do it dynamically is to add padding to the children. You can just set it using .setPadding() on the object to be added. This example is adding an ImageView to a LinearLayout:

LinearLayout userFeedLinearLayout = (LinearLayout) findViewById(R.id.userFeedLinearLayout);
imageView.setImageBitmap(bitmap);
imageView.setPadding(0, 30, 0, 30);
userFeedLinearLayout.addView(imageView);

The following image shows two ImageViews that have been added with padding:

Padding On Children

Solution 13 - Android

If your layout contain labels o some container for text. You can add at the end of each text "\n" to split a line and make space between elements.
Example:

video?.text="Video NR1: ${obj.Titulo} \n" 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionab11View Question on Stackoverflow
Solution 1 - AndroidriwnodennykView Answer on Stackoverflow
Solution 2 - AndroidThomasView Answer on Stackoverflow
Solution 3 - AndroidworkedView Answer on Stackoverflow
Solution 4 - AndroidGorkyView Answer on Stackoverflow
Solution 5 - Androidteh.fonsiView Answer on Stackoverflow
Solution 6 - AndroidChris FeiView Answer on Stackoverflow
Solution 7 - AndroidFrancis BaconView Answer on Stackoverflow
Solution 8 - Androidandroid developerView Answer on Stackoverflow
Solution 9 - AndroidOleg NekrasovView Answer on Stackoverflow
Solution 10 - AndroidSinan AktepeView Answer on Stackoverflow
Solution 11 - AndroidHarish RanaView Answer on Stackoverflow
Solution 12 - AndroidConcernedHobbitView Answer on Stackoverflow
Solution 13 - Androidjan4coView Answer on Stackoverflow