I have designed 2 xml files, one portrait and another for landscape for a single activity. I have called one xml file using setContentView(R.layout.portrait); in my onCreateclass. How should I call xml file for landscape.
You should have the same file name for both layout resource files. The only difference is that the landscape layout folder will be called "layout-land" and inside you'll have the layout xml file in landscape mode. When the app is running, Android knows to take the correct xml file and set the layout according to the current mode so you don't have to worry yourself about selecting the proper xml layout file.
Related
I have the following layout:
I need the following layout in portrait mode:
But instead of that I obtain the following one that I don't know how to modify it:
How can I do that?
Fastest way for Android Studio 3.x.x
1.Go to the design tab of the activity layout
2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation
Create a layout-land folder in res folder and create xml with the same name in this folder.
You could create a seperate Layout for Landscape mode.Just name it the name as your current layout but make sure to add it in the folder layout-land.
Right click on res - layout and then Add New and for the folder write layout-land. Name should be - as said before - the same as your current layout.
When now changing the orientation to landscape, Android will use this layout.
Just create layout-land folder near layout folder and create xml with the same name.
No need to create layout-land. Google has an open sourced FlexBox Layout that works adaptive on screen size /orientation changes like CSS.
Have a look at google's developer site for examples.
I have an activity inside it I have a fragment (Rectangular Box). My landscape layout is not showing even if I had created. My portrait xml file is like this.
First one land and second one is portrait and the down is the Result
but when I turn my emulator to landscape mode it show as in Result in the image.
but when change android:screenOrientation="landscape" in manifest file, it comes perfect as I want but if I use android:configChanges="orientation|screenSize.
It shows the same problem.
Make a folder inside res/layout-land and put the landscape view xml file inside this folder with the same name.This will Solve your problem.
I am using android 4.2 when i am trying to run my program it's working only in portrait mode. Not displaying anything in landscape mode.
I have also used
http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts
http://android.programmerguru.com/android-landscape-layout-example/
but still no success.
Steps i followed
created one more folder with layout-land and keep in same where layout is present.
copied main.xml from layout and pasted in layout-land folder.
added one more button in layout-land -> main.xml.
running in my android device but it's showing only in portrait mode.
please tell where i made mistake ?
Make sure both XML files present in two different folders(namely layout and layout-land) have same name.
try this in your manifest to open activity in Landscape mode
<activity
android:name="com.example.newdatabase.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape">
created one more folder with layout-land and keep in same where layout is present.
copied main.xml from layout and pasted in layout-land folder.
added one more button in layout-land -> main.xml.
running in my android device but it's showing only in portrait mode.
You did it wrong. layout-land should only be used if your landscape layout is different in landscape from what layout is is considered "default" one (which is the one in layout folder). If you do not need different layout, then just keep one file in layout folder and it will be used in either portrait or landscape mode.
You may also want to check your Manifest file and check if you by any chance do not enforce portrait there. ALso "shows nothing" is quite odd - I'd rather bet that your default layout is rendered incorrectly in landscape mode rather than android failed to show anything.
I am trying to simply alter the setlayout when I rotate my device so that I can have a layout of Views for a particular activity that is suited to the current orientation of the Android device but I am confused about the best way to achieve this.
I have referred to the following android doc:
Handling Runtime Changes
I do not need to save any data from my Activity so don't think I really need to use the onRetainNonConfigurationInstance() method. I tried handling the orientation change myself through the onConfigurationChanged() method, where I find the current orientation then set the layout as required but this results in views that no longer work. Is there something else I need to do in onConfigurationchanged()?
Thanks
To get a different view for landscape as opposed to portrait, you would place your layout XML file in both of the following resource folders:
/res/layout - Portrait
/res/layout-land - Landscape
This is, of course, if you have the same views within both, otherwise you may get some NullPointerExceptions.
You can create a new directory under res called "res\layout-land", create an .xml layout file in both "res\layout" and "res\layout-land" that have the same name. For example: "myLayout.xml". Android will automatically use the layout from the -land directory when in landsacpe orientation and the other when in portrait.
in your projects res folder you should have a layout folder. Create a new folder in the res and call it layout-land. Now create your second set of layout.xml files that are specific to landscape oriented devices. Save them in this layout-land folder. The system will handle the rest for you.
check out this page and scroll down to "Providing Alernative Resources" for more detail about different qualifiers you can use on your res folders.
Edit: What device are you using? I created a quick test project that is nothing but hello world but displays different text from a layout stored in res/layout-land folder.
I tried it once with and once without configChanges="orientation" in the manifest. When I run the app and switch orientations the layouts behave as expected. The layout from layout-land is displayed when device is landscape and layout from plain layout folder is shown when device is in portrait.
The device I tested on is Sidekick 4g. Download the test project and report back how it works on your device if you like.
I worked in a tabactivity, I set the gravity for tabs as bottom. If I change the screen from portrait to landscape, I can't see the tabs.
How can I do it? I need an example please.
Thanks.
Assuming you do not have a separate XML file dedicated to landscape format, you might need to create one and specify the landscape-layout look.
Create another main.xml file with the specifics of how your program should look in landscape mode. The file shoud reside inside your res/layout-land/ folder.
When that is all said and done, in potrait orientation, Android will use main.xml found in res/layout and in landscape main.xml found in res/layout-land.