-7

I have two files activity_main.xml and activity_main.xml(land).

If my phone is in portrait mode, I want two run activity_main.xml.

If my phone is in landscape mode, I want to run activity_main.xml(land).

What should I do ?

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Refer http://stackoverflow.com/questions/5407752/android-layout-folders-layout-layout-port-layout-land – nnn Feb 21 '17 at 04:15

4 Answers4

2

Difference is activity_main.xml(land) is for landscape mode. If everything is there make sure your screen rotation is turned on!

Or else if you need to know how to do that,Create a new directory layout-land, then create xml file with same name in layout-land as it was layout directory and align there your content for Landscape mode.

Note that id of content in both xml is same.

Or you can do that in this way,

enter image description here

enter image description here

Now the job is done!

enter image description here

But you have a single Activity and two views depend on the orientation. View A might not have something in view B(if you have the same set of views in both xmls then you don't need this). If you don't have same set you need to initialize your views in the correct way!

When you initialize your views you can do this by:

For Lanscape

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    //init  views in landscape 
}

For Portrait

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
 //init views in portrait
}
Charuක
  • 12,953
  • 5
  • 50
  • 88
0

delete the file activity_main.xml(land) and refer the activity_main.xml to the same java file!

hiashutoshsingh
  • 980
  • 2
  • 14
  • 41
0

Android SDK provides different ways to manage resources of android application. Device supports two orientation mode:

Portrait

If you want to add any specific layout for portrait mode only, You should name layout file as 'xyz-port.xml'.

Landscape

If you want to add any specific layout for portrait mode only, You should name layout file as 'xyz-land.xml'.

Android system will decide runtime which layout to choose.

For your query below,

I have two files activity_main.xml and activity_main.xml(land)

If you do not specify any like, activity_main.xml then, It will be used for both.

For more information check here. Thanks.

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
0

please check the following link i hope you get the solution.

google developer

link 2

Hiren Vaghela
  • 916
  • 1
  • 9
  • 22