Augusto Picciani
Augusto Picciani

Reputation: 804

Android - Manage layouts landscape

I need an activity load different fragments depending orientation:

Activity-> (portrait) Fragment Portrait

Activity-> (landscape) Fragment Landscape

this is activity_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.examples.MainActivity"
android:background="#FF0000">

<fragment
    android:id="@+id/navigationContainerFragment"
    android:name="com.examples.FragmentPort"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 </LinearLayout>

this is activity_layout_land.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.examples.MainActivity"
     android:background="#FF0000">


  <fragment
      android:id="@+id/navigationContainerFragment"
     android:name="com.examples.FragmentLandscape"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

 </LinearLayout>

Then fragmentPortrait.xml

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

   <TextView
      android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:text="FRAGMENT PORTRAIT"/>

</LinearLayout>

And FragmentLanscape.xml

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

  <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="FRAGMENT LANDSCAPE"/>

 </LinearLayout>

Problem is that I do not see FragmentLandscape when rotating. Can someone help me? Thanks

UPDATE: I already put different layouts in different folders: /layout and /layout-land

Upvotes: 1

Views: 50

Answers (1)

Anisuzzaman Babla
Anisuzzaman Babla

Reputation: 7480

Create two folder in res with name layout-land or layout-port

Place Portrait xml file in layout-port:

and

Place landscape xml file in layout-land:

Keep that in mind file both file will have same name

Upvotes: 2

Related Questions