Bharat
Bharat

Reputation: 3000

layout-land not picking landscape layout xml

I want to have a different layout for landscape & portrait. But both the orientations end up using the same layout xml. I've gone though more than a dozen of questions on StackOverflow but none of it solves my problem.

Under layouts I have a activity_main.xml file. I have folders for different landscape layouts as stated in this answer:

layout/  
   activity_main.xml 
   layout-land-hdpi/
      activity_main.xml
   layout-land-ldpi/ 
      activity_main.xml
   layout-land-mdpi/ 
      activity_main.xml
   layout-small-land-ldpi/
      activity_main.xml

These have the same activity_main.xml file except that the background color is different.

I do not have android:configChanges set in AndroidManifest.xml for the Activity. I am not overriding onConfigurationChanged() as I am not manually dealing with orientation changes.

I am using actionbarsherlock library in the project. I made action bar themes using ActionBar Sytele Generator.

minSdk & TargetSdk from AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

I am running the code on Emulators running Android 4.3(Galaxy Nexus), Android 4.2.2(Nexus 7) and Android 2.3.3(Nexus S). The result is the same in all.

Is there anything I am not doing right?

Thanks!

Upvotes: 4

Views: 4822

Answers (3)

Harish Godara
Harish Godara

Reputation: 2386

You must define all your layout-land folder into res folder instead of layout folder in this way :

res/layout-land

not like you did as

res/layout/layout-land 

Upvotes: 1

Vikram
Vikram

Reputation: 51571

You are going about this the wrong way. Folders layout and layout-land should be on the same level.

You need res/layout and res/layout-land.

res
  |_ layout
  |_ layout-land
  |_ layout-...
  |_ layout-...

So, your setup should look like:

res
  |_ layout
          |_ activity_main.xml
  |_ layout-land-hdpi
          |_ activity_main.xml
  |_ layout-land-ldpi
          |_ activity_main.xml
  |_ layout-land-mdpi
          |_ activity_main.xml
  |_ layout-small-land-ldpi
          |_ activity_main.xml

Upvotes: 5

Sushil
Sushil

Reputation: 8488

May be your phone screen density is xhdpi for which you have not kept a folder. Also keep a folder for layout-land-xhdpi and try. you should also keep layout-land-xxhdpi.

Or you can just have layout-land

Upvotes: 0

Related Questions