Android multiple screensize support - android

I need some suggestion with supporting multiple screen size,
Which will work
using
this
values
values-hdpi
values-large
values-ldpi
values-mdpi
values-small
values-xhdpi
values-xlarge
or
this
values-sw360dp
values-sw600dp-land
values-sw600dp-v21
values-sw720dp-land
values-sw720dp
values-v17
values-v21
values-2400dp
values-w720dp-land
values-w720dp
what values folders should i create in the res?i want to know all of it, if i may
Do i need to programmaticly declare or just put #dimens/something in the layout xml?
3.DO i need to make more layout folders to support multi screen size or just one layout folder and using #dimens is enough to handle multi screen size?

First and foremost, the following are depreciated from Android 3.2 (API 13) and up...
values
values-hdpi
values-large
values-ldpi
values-mdpi
values-small
values-xhdpi
values-xlarge
Basically, you only need to use these when you are developing for devices running anything lower than Android 3.2 (API 13).
Smallest Width
values-sw360dp
values-sw600dp
values-sw600dp-land
values-sw600dp-v21
values-sw720dp-land
values-sw720dp
The "sw" means Smallest Width. The OS will pick the one that fits the smallest available width that is available to your activity window. Therefore, if you have a device that is 700dp wide, it will use the resources from values-sw600dp for portrait and values-sw600dp-land for landscape because 600dp is the "smallest width" that you have defined and 720dp. If you wanted, for example, drawables that are used on screens 1024dp or wider then you would create a directory called values-sw1024dp. The naming scheme is as follows: [resourcetype]-swdp-optionalFlag
Note: That optional flag tells the OS to only use resources for that specific instance. For example, the flag "-v21" means that it should only use those resources if the OS is API level 21 or greater.
Available Screen Width
values-w720dp-land
values-w720dp
These specify the minimum available width at which the resources should be used.
The Android docs describe it way better than I can...
This is often useful to determine whether to use a multi-pane layout,
because even on a tablet device, you often won't want the same
multi-pane layout for portrait orientation as you do for landscape.
Thus, you can use this to specify the minimum width required for the
layout, instead of using both the screen size and orientation
qualifiers together.
Supporting Multiple Screens
Question 2 & 3
Do i need to programmaticly declare or just put #dimens/something in
the layout xml?
You only need to put the above directories in your /res directory.
So, for example, your layout directories... you would put something like this in your /res directory:
layout
layout-land
layout-v14
layout-land-v14
layout-sw600-land
layout-sw600dp-land
and for your drawables you might have something like (for pre-3.2 support):
drawable
drawable-hdpi
drawable-hdpi-v11
drawable-mdpi
drawable-ldpi
and for your values:
values
values-v14
values-sw600dp
values-sw600dp-land
values-w820dp
values-w820dp-land

The both can work.
To support multiple screen size, the most common one used in res/value is dimens.xml. (the name of the file is just convention)
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
In other res/value-XXX, the value of activity_horizontal_margin in dimens.xml can be other to fit your need.

Related

Supporting different screens for android

This is my first time working with multiple screens. I want to build my application for multiple screens i.e from sw320dp to sw720dp. I have created the following layout folders.
res/layout-sw320dp
res/layout-sw360dp
res/layout-sw480dp
res/layout-sw600dp
res/layout-sw720dp
I have copied all the xml files inside these folders. Is there anything else I need to add to make sure all the layouts support multiple screens. I have gone through the android documentation but I am not clear with the manifest.xml part. If anyone implemented multiple screen support in their application, so please do provide a description and implementation of the same.
Step 1 -You have to create different values folder for Different values for different screens.
Go to Your Project / app / src / main / res.
Right click on res and create different values folder in it.
Step - 2. Create folders named
values-large
values-small
values-sw320dp
values-sw320dp-hdpi
values-sw320dp-xhdpi
values-sw320dp-xxhdpi
values-sw480dp
values-sw600dp
values-sw720dp
Step - 3. Create dimensions.xml file in values folders.
Different values for different screen size.
values-ldpi 2.7" 240*320 ldpi
values-ldpi 3.3" 240*400 ldpi
values-ldpi 3.4" 240*432 ldpi
values-mdpi 3.2" 320*480 mdpi
values-hdpi 4.0" 480*800 hdpi
values-hdpi 3.7" 480*854 hdpi
values-xhdpi 4.7" 1280*720 xhdpi
values-xhdpi 4.65" 720*1280 xhdpi
values-sw480dp 5.1" 480*800 mdpi
values-sw480dp 5.4" 480*854 mdpi
values-sw600dp 7.0" tablet 1024*600 mdpi
values-sw720dp 10.1" tablet 1280*800 mdpi
when you attach dimension.xml file with your layout than you will get direct effect with your screen size.
This will help you to set dimensions for all type of screens.
There is a difference between supporting multiple screen sizes and creating different layout.xml files for each screen size.
In all the apps I've ever worked on, there were really only three different kinds of screens we cared about: small phones (years-old devices that our users weren't upgrading), "regular" phones (e.g. modern-day Samsung or LG phones etc), and tablets. Even considering those three kinds of screens, we often didn't need to create more than a single layout.xml file for a single screen.
If you have just one layout.xml file, it will display itself on any screen size. To "support" multiple screen sizes, you just need to make sure that your content looks good on short phones and tall phones, on wide phones and narrow phones, on phones and tablets, etc. This generally comes down to using dimensions like match_parent, or layout_weight to fill available space, etc.
It is only when you actually need to change what elements are on screen (as opposed to how big elements are) that you need to create extra layout.xml files. For instance, perhaps you know that a certain set of text + images just won't fit on smaller phones. Then you can create one res/layout/layout.xml that has only the text, and another res/layout-sw360dp/layout.xml that has the text + the image. Or maybe you have some content that you want to display side-by-side on a tablet, but you only want part of it on phones. Then you can make one res/layout/layout.xml with the normal content and one res/layout-sw600dp/layout.xml with the tablet-only content.
Regardless, when you decide that you do want to make multiple versions of a layout for different screen sizes, the only thing you have to do is create copies of your layout.xml in different layout-swXXXdp folders. Don't bother with layout-large unless your app supports really old API levels; the swXXXdp method is much more accurate and solves the same problem (but was only added in API 13).
Hey you dont need to do anything in manifest.
You have done the part with layouts.
Next you can do is to add support in drawable folder i.e. different density images for different sizes.
And if different screens require different values(dimentions etc) you need to create multiple file in values.

Layout folder difference between sw720dp and xlarge

What kind of tablets get under layout-xlarge and layout-sw720dp folders?
For example 8" - 10" goes under layout-xlarge folder. It's from documentation:
But what about layout-sw720dp ?
I know that If I haven't layout-xlarge folder android will navigate 10" tablet to layout-sw720dp, but what about 8" - 9" tablets ?
Android will choose layout or layout-sw720dp folder?
layout-sw720dp means devices with 720dp of smallest width. Specifically, the device's smallestWidth is the shortest of the screen's available height and width.
From Here
smallestWidth
Examples:sw320dp, sw600dp, sw720dp etc. The fundamental size of a
screen, as indicated by the shortest dimension of the available screen
area. Specifically, the device's smallestWidth is the shortest of the
screen's available height and width (you may also think of it as the
"smallest possible width" for the screen). You can use this qualifier
to ensure that, regardless of the screen's current orientation, your
application has at least dps of width available for its UI.
However if you use both layout-xlarge and layout-sw720dp in an app, the devices that qualifies both of them will always take from layout-sw720dp. This is because of the higher precedence for sw<???>dp qualifier. This is clearly specified in the docs which is linked above. If you have given multiple qualifier types for any resources, Android will search for qualifying folders in the order of precedences given to each type.
From the docs
Android supports several configuration qualifiers and you can add
multiple qualifiers to one directory name, by separating each
qualifier with a dash.
Table lists the valid configuration qualifiers, in order of precedence—if you use multiple qualifiers for
a resource directory, you must add them to the directory name in the
order they are listed in the table.
It will pick layout-sw720dp folder. According to screen width it will first look for device width larger than 720dp
The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra-large screen should go in layout-xlarge/.
Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the swdp configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/. Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2.
Reference
https://developer.android.com/guide/practices/screens_support.html
There are multiple folder as bellow you can read about them aswell
1)layout-sw480dp drawable-sw480dp
2)layout-sw600dp drawable-sw600dp
3)layout-sw720dp drawable-sw720dp

what is dimens.xml(w820dp) in android studio.?

Upon creating the new project in android studio in the following path were created two xml files by default.
..res/values/dimens.xml
..res/values/dimens.xml(w820dp)
Am aware of the file dimens.xml where we can define the dimensions for the elements, but why the second file dimens.xml(w820dp) created by default.
Can someone please shed lights on why it is needed and when we can use it.
You can define several folders for each desired screensize in Andorid Studio. In your case, android studio defines values folders where you define dimensions (measurements) for scaling tablets or phones.
E.g. it defines two values folders like:
values-large
values-xlarge
or in your case
values-w820dp
Now inside this folders, a dimen.xml file is defined, where you can put your measurements in (unit dp) for the corresponding screensize.
Define a measurement like this:
<dimen name="value1">17dp</dimen>
Then embedd this sizes in your layout xml, like:
android:layout_height="#dimen/value1"
Depending on the screensize, the system will load the correct measurements from this folders, e.g. if you have a screen size large only the defined values in folder values-large will be loaded.
For more information, also have a look at https://developer.android.com/distribute/essentials/quality/tablets.html
EDIT1: So basically if you see the file dimen.xml(w820dp), the values in there are only used, if the app is executed on displays with 820dp and above, in this case, it means tablets.
In fact you have not file named ..res/values/dimens.xml(w820dp) at all in your project structure (check yourself). What you have instead is ..res/values-w820dp/dimens.xml file but to make editing right file easier, Android studio will (in Android view) show it as such. This file will be used by the framework on devices with display width 820dp or higher:
Specifies a minimum available screen width, in dp units at which the
resource should be used—defined by the value. This configuration
value will change when the orientation changes between landscape and
portrait to match the current actual width.
On other, "plain" dimens.xml will be loaded. See Providing Alternative Resources documentation on resource qualifiers to find out more about possible options and benefits.
Main use case of these files is to Support variety screen resolutions for user interface
dimens.xml
Android automatically take values from this file for device with dpi less than 820dp
dimens.xml(w820dp)
Android automatically take values from this file for device with dpi greater than 820dp
How to use
Declare dimension in these files with same name(dimens.xml)
<dimen name="button_width">200dp</dimen>
<dimen name="button_height">40dp</dimen>
Change only in their values (dimens.xml(w820dp))
<dimen name="button_width">400dp</dimen>
<dimen name="button_height">80dp</dimen>
w820dp
for screens with more than 820dp of available width. This would include 7"
and 10" devices in landscape (~960dp and ~1280dp respectively)
w820dp is a qualifier value to provide alternate values for different dimensions. This is used to localize to device types properly.

Android devices with different height takes same layout folder

I have a Micromax AQ5000 with Screen Resolution- 1280*720 pixels and Motorola Moto G XT1033 with resolution 720 x 1280 pixels.I have a layout folder named layout-sw360dp which I designed for devices like Samsung s4,s3,Micromax canvas etc but this Motorola device is also using the same layout and this creates the images displayed as distorted in it.
How can I create a folder for the small device(Moto g) I tried layout-xhdpi but it doesn't work how can I name layout with height and width.
Well you are right in some sense android should take layout dependent on different densities but some mobile do not fall under specific density. Therefore android will pick up default layout from layout directory.
to support multiple screen resolution provide different layout for different screen sizes, you can make following directories in res directory like this
layout-hdpi
layout-mdpi
layout-xhdpi
layout-xxhdpi
layout-w320dp-h408dp
layout-w480dp-h800dp
layout-w480dp-h854dp
layout-w720dp-h1280dp
layout-w1080dp-h1920dp
when you provide layout in all this directories you will give multiple screen support for different sizes as well
layout-w1440dp-h2560dp
Use "dip" instead they will help you in debugging your layout as they will try to keep a coherent size to multiple screen resolutions,
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="45dip"
android:layout_height="45dip"
android:src="#drawable/avtar011"/>
while supporting multiple screen when you give "dp" to dimensions, Actually android expects you to provide different values for different screen resolution. Lets say below is your imagview dimensions make few folders in res folder in your android project like these below
values-hdpi, values-mdpi, values-ldpi, values-xhdpi, values-xxhdpi
and in them make a dimens.xml file each of them and write
<dimen name="image_view_width">28dp</dimen>
<dimen name="image_view_height">28dp</dimen>
now that i have mentioned "dp" here instead of dip android wants me to keep track for different dimensions for different screen resolution, So i will change the image_view_width and image_view_height values are in separate values folders where dimens.xml is located. Make sure your dp values change as per your screen resolution you want your view to fit in.
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="#dimen/image_view_width"
android:layout_height="#dimen/image_view_height"
android:src="#drawable/avtar011"/>
hard part is over now android will pick one of dimens.xml values depending on which screen your app is running, Voila now your layout rocks

Android Studio 0.8.6 multiple values folder

I am using multiple values folders like values-hdpi ,values-xhdpi , values-xxhdpi folders to support multiple screen sizes. Lets say I have a dimension named "listitemheight" in all that values folders with different density pixel values.My question is, when I select between Nexus S(hdpi) and Nexus 5 (xxhdpi) Design rendering of Android Studio IDE does not pick specific dimension for the screen size .Is there any way to do that correctly ?
In values-hdpi
<dimen name="listitemheight">30dp</dimen>
In values-xxhdpi
<dimen name="listitemheight">70dp</dimen>
EDIT : It always chooses hdpi folder
Try using folders with names like "values-w600dp" . I had the same problem with you and this was the solution for me.
Starting with API Level 13 (Android 3.2), the screen sizes are deprecated in favor of using the swdp qualifier. This new qualifier declares the amount of space a given layout needs. It is strongly recommended that applications that are meant to run on Android 3.2 or higher should be using these newer qualifiers.
For example, if a layout required a minimum 700dp of screen width, the alternate layout would go in a folder layout-sw700dp
Source : http://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_4_-_creating_resources_for_varying_screens/

Categories

Resources