'How to include a portrait layout in a landscape layout?

I have designed the perfect portrait layout. I'd like to include is as a part of the landscape version (see attachment).

Layout for Landscape version

There is no extra functionality, so I don't see a need to go to trouble of using Fragments.

Of course, one cannot include the portrait version of a layout in the landscape version. The following code produces the obvious error of "layouts should not include itself":

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/demo"
        />
</LinearLayout>

Is there a way to force a layout directory so that I don't get this include infinite loop? Or is there another way altogether? I'd really like to avoid changing any java code at this point!



Solution 1:[1]

Best practice:

  • Put your content layout into a orientation-less layout file. (this is the reusable part)
  • Create a frame layout file for all orientations you want to use and include the content layout. (this is the part where you put layout-specific parts, e.g. include more stuff in landscape)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1