i want to have two buttons with vertical orientation when the device is in portrait mode,and in horizontal orientation when it is in landscape.How will i get it with only an .xml file?May i use something like "styles.xml" and say that if i have portrait do this and if i have landscape do the other one?Thanks
The layouts in /res/layout are applied to both portrait and landscape.
To use a different layout in landscape, create a folder /res/layout-land and create another xml file here with the same name. Android will automatically choose the layout corresponding to the current screen orientation.
EDIT :
If you cannot create a second folder or are concerned about performances, then you can override the onConfigurationChanged() method, as explained here.
Related
i have two layouts here one is portrait and one is landscape the landscape is located in the layout-land folder and the the portrait is located in the layout folder both have the same name. what i want to do here is that when the emulator screen is portrait it will load portrait and when the screen is landscape it will load a landscape layout but it is not happening when the first view is portrait and when i change orientation it still loads the portrait layout in landscape view and when its landscape first and i change orientation to portrait it loads the landscape layout on portrait screen what seems to be the problem here.
portrait code: http://pastebin.com/QSVL2MHf
landscape code: http://pastebin.com/ktGAe0QX
Remove android:configChanges="orientation" in your AndroidManifest.xml section for this particular Activity.
I have implemented an application for android 7 incs tablet.In my application i have used two types of layouts in layout-land and layout-port directories with same xml name and different designing.
In this application the tablet view is in landscape mode as by default when i change the orientation of emulator the the view is calling portrait xml from layout-port and if again change emulator orientation it is not getting xml view from layout-land.
I have implemented my application as follows:
if(wm.getDefaultDisplay().getOrientation()==1){
//portrait view code
}
else{
//landscape view code
}
from the above logic i can get the view from land scape to portrait but i can't get same view portrait to landscape.
How can i get my land scape view when i change landscape to portrait to landscape?
please any body help me?
That seems to be a problem with the emulator. You don't have to implement from code the orientation changes, since the system have by default this behavior. Just place your xml files in layout-land and layout-port and on a real device it will work fine.
The bug has been reported and you can see this here
You can use this code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You maybe also have to set this in your manifest under activity:
android:configChanges = "orientation"
I want to desing a GUI for android's tablet. By default i want to use landscape orientation of this application, so i placed the controls (text boxes, text views etc) accordingly, but when i change the orientation of tablet from landscape to portrait, whole GUI of activity gets distrubed. Can anybody guide/help me what is the best approach for designing of GUI in android such that controls appearance should remain correct in either orientation (portrait or landscape)? Thanks in advance.
You have to create another folder in the res directory called "layout-land". Put the layout file for landscape in this folder and the one for protrait mode in the normal "layout" folder. Both files must have the same name. Android will automatically chose the correct layout depending of the current orientation of the device.
Check the link below, it will provide all information about how to design an application to support multiple screens with different sizes and resolutions.
Supporting multiple screens
I am designing with LinearLayout which is working for portrait but landscape it does arranged properly. I do not use x,y positions. If I change anything in xml it affects the design -- how do I design both portrait and landscape?
You can specify a layout-land folder, to put all your landscape layouts (duplicate them, but with little differences in order to they show properly in corresponding orientation). In order to avoid this, you should work with RelativeLayouts. But if it's too late and you don't want to change everything, you can create the mentioned folder and make the proper layout changes.
So you will have a
layout/activity.xml
and a
layout-land/activity.xml
If your app is big, it might become a pain to make every change two times, but it's not so bad.
Not recommended, but you can also avoid changing orientation by supporting only portrait orientation in your app: you would have to put "android:screenOrientation="portrait"" in your app's Manifest then.
You can define a second xml file that is the layout for the landscape version. That file must be named the same and placed in a layout-land folder:
i.e.
Portrait:
layout/main.xml
Landscape:
layout-land/main.xml
I have layouts for portrait mode (in folder layout) and for landscape one (in the folder layout-land). This screen is located under one of the tabs, so I had to handle screen rotating (after screen has been rotated, no default screen restart occurs).
I would like to update view from 'layout-land' when screen rotates to landscape mode and from 'layout' when screen rotates to portrait one.
How can I do this?
You can use onConfigchange method in that setContentView(R.layout.name) this way will work.