Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Using Custom Styles and Themes Using Includes for Repeatable Layouts

Problem with <merge />

I've modified projects layout to use "include" for title and subtitle. But encountered a problem with "merge" in extracted layout file. I have signup_layout file and auth_heading file. I want to include auth_headinginto signup When I try to use like this:

<include
        android:id="@+id/authHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/auth_heading"
        />

    <LinearLayout
        android:id="@+id/loginEditTextGroup"
        android:layout_below="@id/authHeading"
        style="@style/AuthCredentialsContainer">

the LinearLayout goes up to the top and I can't put it under the "include" (it overlaps). But when I use "LinearLayout" instead of "merge" in auth_heading it works fine. Is there a way around? as I understand "merge" is preferrable.

Here is auth_heading.xml and this doesn't work:

<merge>
<TextView
    android:text="@string/app_name"
    android:id="@+id/titleLabel"
    style="@style/AuthHeading"
    android:layout_gravity="center_horizontal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/subtitle"
        android:id="@+id/subTitle"

        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"/>
</merge>

2 Answers

Andrew Sheragy
Andrew Sheragy
16,379 Points

I would just restructure stuff so there is a LinearLayout somewhere to avoid using layout_below or layout_above, the root of everything can be a RelativeLayout but if you need ordered items put them in a sub LinearLayout.

The merge layout is inserting 2 TextViews inside your signup layout so your "android:layout_below" loses its meaning, instead of being below a single "include" item its now below 2 textviews, which doesn't make sense for that parameter. Here is a better way of doing it

<RelativeLayout> <LinearLayout> <include ...> <LinearLayout loginEditTextGroup>

Andrew Sheragy
Andrew Sheragy
16,379 Points

What is the root layout of signup_layout? If its a RelativeLayout you would get the problem you describe. It should be something like this

signup_layout.xml <LinearLayout> Include ...Some other stuff </LinearLayout>

auth_heading.xml <merge> ...Some stuff </merge>

Yes, the root layout of signup is RelativeLayout. So there is no way around this problem? merge can be used only with LinearLayout?