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 ?
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 ?
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,
Now the job is done!
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
}
delete the file activity_main.xml(land) and refer the activity_main.xml to the same java file!
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.