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
Related
I created layout-large, layout-normal and layout-xlarge in the res folder and I copied all the xml files to those layout folders
First, I want to ask what is the difference between layout (the default) and
layout-normal folder?
I know if I run the application on big screen size the app will take the xml files from layout-xlarge.
So I made all the elements in layout-xlarge look as I want using FrameLayout in tap2 10.1,
but when I run it in note2 or s3 mobile it looks different because the screen size is not same.
So how can I make the the application run in tap2 10.1 (1,280 x 800) and note2 or s3 mobile (1,280 x 720) size?
Here there is the official description of how support all the screen size. And as described the layout folders are:
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
So layout and layout-normal folder are the same, but in the new Android API there isn't no more the layout-normal folder, but only layout folder. Also you have to know if there isn't the specific layout of the screen size in witch the app is running, by default the app takes the "default" layout.
EDIT
You can have different screen resolutions in the same folder, as you can learn from this images:
If you want a more specific layout for specific resolution you have to detect at run time the exact screen size.
From Supporting Multiple Screens, you should define layout folder like this
res/layout/my_layout.xml // layout for normal screen size ("default")
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
(the screen type base on)
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
I have tried to use
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
When i launch my app it is able to see the res/layout-xlarge/my_layout.xml but when I turn the orientation (the view does turn), it doesn't show the res/layout-xlarge-land/my_layout.xml but the portrait one instead. (sorry for my bad english)
According to the documentation xlarge resolution requires a minimum of 960dp x 720dp
http://developer.android.com/guide/practices/screens_support.html
Since you have only defined a landscape layout for xlarge, it's possible that your device does not have a large enough display to use it.
I would suggest adding
res/layout-small-land/my_layout.xml
res/layout-large-land/my_layout.xml
Additionally you can determine the size category of your device using the answer to this post:
How do I get the ScreenSize programmatically in android
Not all orientations are supported in all versions of the SDK. I could not find the information saying what SDK version xlarge-land is available in but I would bet if you change your minimum SDK version to 16 and target building to 16 it will work. Try going from there and see if you can find when it stops working.
Related: http://developer.android.com/guide/topics/resources/providing-resources.html#Compatibility
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 :)
I'm looking to make an Android app that changes the look of the UI based on the device it's shown on (4 inch phone, 7inch tablet and 10inch tablet). I know you have the fragments API and it's probably that which I should use for this, but all the examples I've seen just have 2 activities sitting side by side and the code is a bit OTT for my needs (they have events passing between them etc).
Here is the comparison between a 4 inch device and a 7 inch device and how the different UI elements should move around (also note that element A can change design between the two devices).
Any ideas on how I would go about doing this, or if anyone has any sample code that would be fantastic!
I think you should probably design different xml for each size. You can use the qualifier in the layout dir for different screen size. http://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources
You can use different directory for different size :
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
and for more information :
http://developer.android.com/guide/practices/screens_support.html
Good Luck,,, :D
See also Fragments for Android 3.0+.
I am making an app that is readily available and MOSTLY used on phones, but would be CONVENIENT to use on a tablet if available.
I am familiar with landscape/portrait layouts for the separate XML files, but what about for the different resolutions? This is also an issue for phone-only applications, what is the best way to have scalable layouts.
I use the dip values which are density independent but not as effective as just using percentages that will scale your layout effectively.
I also hardcode the XML. I IMAGINE that I could programmatically generate the layout by checking the screen width and height ON EVERY ACTIVITY, but tell me there is a better way?
You can provive extra layouts by specifying the screen size (small, normal, large and xlarge) in the same way you specify layouts for portrait or landscape.
These are some examples taken from the android documentation related to this subject
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
The book The Busy Coder's Guide to Android Development has an excellent explanation about this subject.