Margin does not impact in "include"

AndroidAndroid Layout

Android Problem Overview


I have a view with articles. It uses "include", and I'm trying to make a little margin between them. However, "android:layout_marginTop" does not seem to have any impact on the layout.

What am I doing wrong?

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >    
    <include android:id="@+id/article1" layout="@layout/mainarticle" />
    <include android:id="@+id/article2" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article3" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article4" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article5" android:layout_marginTop="10dip" layout="@layout/article" />    
</LinearLayout>

Android Solutions


Solution 1 - Android

You should add the android:layout_width and android:layout_height attributes in the include tag. Otherwise, the margins are not taken into consideration.

> However, if you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.

https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include

Solution 2 - Android

I had the same problem and the answer from Kamen Goranchev doesn't work for me.

I have used ADT's feature "Extract include..." from the layout editor to extract some commonly used badges as a list of TextView-elements. So the Extract-include-tool wrapped my TextView-Elements in a merge-tag, which usually would be fine.

But, according to the very helpful sourcecode-link from boiledwater I see in line 888 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L888, the layout-attributes from the include-tag itself are only parsed if the include doesn't have the merge-tag as its root-element.

So I removed the merge-tag from the include and used another ViewGroup-tag like e.g. FrameLayout. Then the margins in the include-tag are working as expected.

Solution 3 - Android

include tag support below properties:

  1. Any android:layout_* attributes which you can overwrite.

  2. android:id attribute.

  3. layout attribute.

  4. android:visibility attribute.


Etc: include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>

Please read: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L777
http://developer.android.com/training/improving-layouts/reusing-layouts.html

Solution 4 - Android

Another solution would be to add Space before include:

	<Space
		android:layout_height="8dp"
		android:layout_width="match_parent" />

Solution 5 - Android

must insert include into other layout.

e.g. Relativelayout

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp">

        <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/sub_edit_new_customer" />

    </RelativeLayout>

Solution 6 - Android

In my case I solved the problem by adding some padding.

The layout you want to include:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"> <!-- add padding here -->

        <!-- your custom layout -->

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

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
QuestionohadinhoView Question on Stackoverflow
Solution 1 - AndroidKamen GoranchevView Answer on Stackoverflow
Solution 2 - Androidarne.jansView Answer on Stackoverflow
Solution 3 - AndroidboiledwaterView Answer on Stackoverflow
Solution 4 - AndroidStamView Answer on Stackoverflow
Solution 5 - AndroidAli BagheriView Answer on Stackoverflow
Solution 6 - AndroidLaufwunderView Answer on Stackoverflow