i added in manifest file.I craeted two folders layout-land and layout-port,eg:main.xml in both folder, i stored xml file in corresponding folder.when i change to landscape mode it is calling layout-port containg xml.
See http://developer.android.com/guide/practices/screens_support.html
Defined them as different layouts and put them in the appropriate folders and the os will handle it for you
You would be make different xml file as like drawable folders (landscape, portrait) as below :
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
If you are in landscape or portrait mode, Android looks for the layout file in either the -port or -land directory first, if it's not found then it falls back to the default layout directory.
Read more about resources here.
FYI the Android Emulator (AVD) isn't able to recognize it's in landscape. Even if you press CTRL + F11 and it turns the AVD emulator sideways, it doesn't actually register with the appliction that it's in landscape mode. So in short, if you're running on the emulator, your application will always think it's running in portrait mode (and not look for any "layout-land" resource folders). Test it on and Android device and it should work perfectly :)
Related
Is there a way to lock different screen orientation for different screen sizes? So for screens with smallest width smaller than 600 dp to lock portrait orientation and for screens with smallest width greater than 600 dp to lock landscape orientation? I understand that this can be done programmatically with setRequestedOrientation, but can it be done in xml resources somehow? In Android.manifest, or by referencing different resource values defined in res folders?
Option 1: Use the following code for each activity inside AndroidManifest.xml:
android:screenOrientation="nosensor"
The "nosensor" setting tells your app to use the natural orientation for the device, and will not change if the user rotates the device. So tablets will use landscape orientation for your app, and phones will always use portrait orientation.
Next you can create a separate layout file for landscape native devices (i.e. tablets) by creating a folder called layout-land in your res folder. Put the XML layout files for tablets into the layout-land folder.
Option 2: Use the smallest width qualifier, as described in the Android developer documentation here: http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
In this case you will create a layout folder in the res directory called layout-sw600dp. Inside there you will put your alternate XML files for devices with more than 600dp. In the top level of the XML files for your linear layout, or relatively layout, for example, be sure to include the following:
android:orientation="horizontal"
In your main XML files for devices with less than 600dp, you will force them to vertical orientation:
android:orientation="vertical"
I that When starting an android application, android makes a good "guess" as to which layout/drawable to use depending on the screen size. However, I was wondering where it chooses this from? I ask this because I have quite a few layouts which don't all work. I read on the android website that you can make a folder called layout-xhdpi, layout-ldpi... etc. But does android automatically look for layout-xhdpi or layout-ldpi or do I need to specify that anywhere?
Same goes for drawables. When creating a new application in Eclispe it makes automatically drawables folders for you but does this necessarily mean it looks for those.
Conclusion:
Where does android look for alternative screen sizes and how/where can I change that?
Create following layout folder
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
I implemented all layout files for hdpi devices. And put them in "layout" forder. Now I tested on a normal-screen device, and there's some screen I urgently need to change, because a required input field doesn't appear (because of the small screen).
What I did is create a folder called "layout-normal" and put there a copy of my layout file, and reduce in the copy everything a bit so it fits the screen. Now I have 2 problems and don't understand anything.
Both files point to the same source - when I open them I don't get 2 files, but only 1 with my modifications.
Ran this on the normal-screen device and it still looks like at the beginning.
Please help :/
layout-normal and just layout are the same. It is just the default layout
Ref: android developers site
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
..
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
I am wondering how to get different XML layout files to be read depending of density of the screen.
Now when I enter a layout file and set to so that it has a couple of buttons and have them set to a specific position, the position changes when I change to another screen density in the emulator. This is what should be happening what i understand- but How do I get the program to use different layouts depending of densities?
I have been reading on android dev. page on how to develop for different screens, but I didn't get the examples which were there.
I have been using dp/dip on sizes so no need to suggest that =)
you can do it by creating a layout for each standart screen sizes,
you can find here how to do it.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
it means you should create sub-directory named as ("layout", "layout-small","layout-large","layout-xlarge","layout-xlarge-land") in res folder.
than create a main xml for each one with the same name.
By placing the layout XML files in different folders you can get Android to load the correct one depending on the screen density. For example, if you layout is called "main.xml":
Placing the file in /res/layout-ldpi/main.xml will mean it is used only in low density (or above)
Placing the file in /res/layout-mdpi/main.xml will mean it is used only in medium density (or above)
Placing the file in /res/layout-hdpi/main.xml will mean it is used only in high density (or above)
Placing the file in /res/layout-xhdpi/main.xml will mean it is used only in extra-high density
You can do the same sort of thing with all resources- drawables, strings, dimensions etc. (e.g. drawable-mdpi, or values-en [values only to be used in an English locale]). You can find the whole list of different device set-ups supported by this system at http://developer.android.com/guide/topics/resources/providing-resources.html (Table 2: Configuration Qualifier names)
I have developed an android application, which only supports portrait orientation, for android phones. Now, I need to modify this app because I want to do the universal version (for tablets, too) but I have a problem. The phone version only supports portrait orientation and the tablet version supports portrait and landscape orientations. Is there any way to define different orientations for tablets and phones?
Thanks
I believe you can create a subfolder in the res/layout folder, called xlarge-land and xlarge-port with the xml files in them. the device itself will know what xml to load.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
resource: http://developer.android.com/guide/practices/screens_support.html