0

Is there a way to reuse a single activity layout-file (one orientation) for another activity with two orientations.

for example I have the MainActivity with one layout-file that just shows everything fullscreen:

activity_generic.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MergeRootFrame">

</FrameLayout>

MainActivity.java

setContentView(R.layout.activity_generic);

And then I have SettingsActivity which should show everything fullscreen in portrait-mode and everything splitscreen in landscape-mode.

activity_settings.xml

//use activity_generic.xml somehow

activity_settings.xml (land)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false">

<FrameLayout
    android:id="@+id/content_left"
    android:layout_width="0dp"
    android:layout_weight="40"
    android:layout_height="match_parent"/>

<FrameLayout
    android:id="@+id/content_right"
    android:layout_width="0dp"
    android:layout_weight="60"
    android:layout_height="match_parent"/>
</LinearLayout>

SettingsActivity.java

setContentView(R.layout.activity_settings);

Of course there is a way to fix this problem within code by checking the orientation and inflating another layout based on that, but it feels kind of hacky. I tried to use an include tag but it didn't work. Does anybody else know the solution to this problem?

Edit

I managed to edit the activity_settings.xml so that it uses an include tag. However it is a bit overkill because it uses almost as much code as the code from activity_generic.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
       <include layout="@layout/activity_generic"/>
</LinearLayout>

At this point i'm not sure if its even possible to use activity_generic.xml instead of activity_settings.xml (portrait).

Wirling
  • 4,810
  • 3
  • 48
  • 78
  • `I tried to use an include tag but it didn't work` Please elaborate how it didn't work. Update your question with relevant parts of the XML. – Sufian Feb 25 '16 at 14:01

1 Answers1

0

You cannot use the exact same layout and achieve the same. However there are some built in features that android has and are a common practice.

By default android shall pick the layout files from the /res/layout folder, for both landscape and portrait orientations. However, you can make a /res/layout-land folder and place a layout file with the same name and make landscape related UI changes there. Android shall automatically pick this layout file for the landscape orientation.

You can check this link for further details: Android: alternate layout xml for landscape mode

Community
  • 1
  • 1
Msk
  • 857
  • 9
  • 16
  • Yeah, I did put the layout-land in my example as well, see the (land) after activity_settings.xml. Unfortunately this will cause lots of duplicate layout-files for the portrait variant, cause it will mostly be the same. – Wirling Feb 24 '16 at 13:25