Android conditional layouts for phones:tablets - android

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.

Related

android multiple screens compatibility

I read info about "Supporting Multiple Screens" and other post here... but Im really confused about how can I develop my application that run on multipple devices.
I was starting develop on a determinate screen (normal size layout), then I run my apk on a S4 galaxy so I see that every object of my apk was diferent size to my xperia to the S4.
what I need to do to make my apk compatibility for all the devices?
I read information that the only source to make that is:
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
For:
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
is this true? so I need to make different layouts for different devices?
guys please help me with this issue cz I relly dont understand how is the deal for multiple devices compatibility.
For the layouts:
You can most likely get away with one set of layouts for your application. By default, Android's gui system is done in such a way that you should be able to make good use of the screen of any device, for example use layout_weight and specify things in dp (density-independent pixels). By providing only one layout though, you're most likely going to have it optimized for phone or tablet sized screens. If you create it for phones, then tablets will probably have a lot of empty space, whereas creating another set of layouts would allow you to put extra info on the screen than the phone layouts could fit. It depends how willing you are to customize your app experience for each users device. To create multiple layout, it's recommended to use the Android 3.2 size qualifiers. For tablets, you could use res/layout-sw600dp. See here under "Using new size qualifiers".
For the drawables:
You will most likely not want to have just one set of drawables. You can, and the Android system will scale all your drawables to maintain the same physical size on all screen sizes, but due to scaling, the images will not look very good. That's why you provide an image that will look good for each "category" of screen density, and the Android system will choose the one for that device's screen density, and any scaling it has to do will be minimal.
It is kind of confusing at first, but you most likely don't have to worry about the layouts specifiers just yet. I would make sure to read the supporting multiple screens article and writing your xml layouts such that they make use of the screen size in a relative way.
Have you read this page? Or this one?
You create resources with duplicate names located in folders that are qualified a certain way. At runtime, the system will choose the resources that best match the current configuration of the device. If you want your device to work on multiple screen sizes and multiple screen densities, you will need to create different resources (layouts, drawables, etc) and qualify them appropriately.

Android build Layout for all device

it is possible to build layout (ex: menu)
that will use coordinates system pt
I need to build menu for game and I need to set all fields on same positions on every screen size
if screen will be big it must be scale everything on the layout and same on small screen size
like this:
can you provide some example for it?
The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.
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
This has been taken from http://developer.android.com/guide/practices/screens_support.html
If you sort your xml files this way, Android itself figures out what file to choose depending on the screen size of the device.
While designing the desired layout, you can opt for Relative Layout which usually sticks to its attributes such as alignParentBottom, alignParentTop, alignParentRight, alignParentLeft to name a few.
I hope this solves your issue, although I'm not clear what you're talking about when you say "coordinates system pt"
create the image with 9-patch so no need to save the images for different density like hdpi,mdpi etc. it will adjust with control as you define the size or you can save images with different size into the hdpi, mpdi with same name and by runtime in device it will get the appropriate images as per the density available
edit
links for 9-patch
http://developer.android.com/guide/developing/tools/draw9patch.html
http://android10.org/index.php/articlesother/279-draw-9-patch-tutorial
http://www.androiddom.com/2011/05/android-9-patch-image-tutorial.html

XML for all screens

I have a layout with 8 buttons 2 image views and 2 text views. On my device (Galaxy S) it fits but on devices with smaller screen the bottom side is not appearing.How could I adapt my layout for all screens?
if you want your app to support all kind of screen sizes,
you need to create different layout xml file for each kind of the screen types.
place each one of the above on resource 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
on run time - android will automatically choose the right resource according to the device's screen type
you can read more about it on -
http://developer.android.com/guide/practices/screens_support.html
use RelativeLayouts or LinearLayouts with layout_weight property. If you can post some xml, i would give you more suggestions.
The problem occurs generally with the ImageViews mostly, as they occupy more space on smaller screen, since smaller screen has lesser no.of pixels per inch. It's a general practice to restrict the sizes in dp.
You can define more layouts with different width and height.Then check the height and width and density of device and use that specific layout for that device.
Use screen dependent pixels dp and sp. Design a different layout for different screen sizes. http://developer.android.com/guide/practices/screens_support.html <-- refer here
For most width and sometimes height problems you can use layout_weight to assign the exact ratio catered for each View.
If there is no other choice and that your layout have to be that way you might want to consider using a ScrollView to wrap your parent layout so that they can be scrollable. http://developer.android.com/reference/android/widget/ScrollView.html <-- refer here.
Good luck.

Android UI design based on screen resolution

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+.

Testing Android application with different-sized screens

I have a question for you guys about multiple screens.
I am testing an Android application for different-sized screens using emulator. The application have been initially developed for resolution 320*480. I know it is not good and I have problems in my code. Moreover, I have used pixels in some places of code instead of dip. You see, I am not a professional in Android. In any case, there is no problem for devices with resolution 320*480 and 480*800. For the second one the application looks stretched. According to documentation it should be so. But, when I am trying resolutions 540*960 or 1024*600 the application occupies just a part of screen and it seems to be 320*480. The question is: why it didn't stretched the application to the whole screen? How can I do it?
I have read Supporting Multiple Screens guide here: http://developer.android.com/guide/practices/screens_support.html#dips-pels, but I haven't found an answer for my question.
Have you go through this lines in the link given by you?
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 i
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
for different size devices, make different folder as given above like layout-small,layout-large.. and put your designing xml in that.
Hope this help you.

Categories

Resources