I have two devices of hdpi and xxhdpi screen density. All of my xml files are in the 'layout' folder. Since the hdpi device's screen is small, I copied some xml files to 'layout-hdpi' folder and made the respective changes for the hdpi device. The problem is that the changes I made for the hdpi device are getting reflected even on my xxhdpi device.
What can be the problem or am I doing something wrong?
Since the hdpi device's screen is small, I copied some xml files to 'layout-hdpi' folder
Screen size is not related to screen density. Do not create layout directories based on density. Create layout directories based on screen size (e.g., layout-sw640dp for layout resources to be used on devices whose smallest width is 4" or larger).
Related
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.
i am creating simple app to show notifications. i want large font on large devices and small fonts on small devices. Please Help.
Following is screen shot of tablet of dpi - 213
Following Screen shot of Moto G3 phone have dpi - 320
You need to create different values folder as per required screen sizes and resolution and put 'dimens.xml' file in each folder.
For example:
For HDPI device- you need to create folder with this name
"values-sw320dp-hdpi" under this folder put "dimens.xml"
For Large HDPI device- you need to create folder with this name
"values-large-hdpi-1024x600" under this folder put "dimens.xml"
For MDPI device- you need to create folder with this name
"values-sw320dp-mdpi" under this folder put "dimens.xml"
Similarlily you can create different values folders for other devices too under "res" folder.
Now, what next to do and How it works:
Say, you have TextView defined in your xml and you need different font sizes on different devices:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/textSize" // This is the important line
android:text="Hello"/>
Now, here 'dimens.xml' file comes into picture. You need to specify different size value of "textSize" under different dimens.xml w.r.t screen sizes and resolution
For Example:
For HDPI device: res>values-sw320dp-hdpi>dimens.xml
<dimen name="textSize">19sp</dimen> // Setting size of Text for HDPI devices
For MDPI device: res>values-sw320dp-mdpi>dimens.xml
<dimen name="textSize">15sp</dimen> // Setting size of Text for MDPI devices
Similarly, you can define sizes for other screen too.
You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.
Then, when you are in medium density device it will pick the file in "values-mdpi" folder if exists or in "values" if not, and the same for high density etc...
This same principle applies to al "res" subfolders
I am in the process of creating layouts for a new application and I am interested to know how you are usually approach this process.
Obviously it's a very general question, but I think we can get a lot of ideas from different developers' perspectives. As creating perfect layouts in Android, in my opinion, is one of the most difficult tasks in the app development process, I think the answers can be of value.
The assumption is that we want to support all device sizes with the least number of XML files, without compromising the application design quality.
In my case the designer of the app designed it with Galaxy S3 dimensions in mind (720 x 1280px).
Considering the 3:4:6:8 ratio -
What will be the base device you'll do the design for, so one layout file will look good on all normal size devices?
How would you handle the difference in device dimensions, thinking in dp -
Eg.
480 X 800 hdpi = 320 X 533dp
320 X 480 mdpi = 320 X 480dp
720 x 1280 xhdpi = 360 X 640dp
How will you still use the extra dp, so there won't be to many empty spaces, without making the layout not appropriate for 320 X 480 device?
When the designer marks a margin of 20px, how would you interpret it in the xml?
Taking into consideration he designed it on a 720 x 1280px canvas.
Start with some screen size as your base target device for e.g. 4.7 inch (hdpi) and create a single layout for default screens in layout folder and declare dimensions in dimens.xml in values folder.
Next, let's say you want your layouts for a 7" device, create values-sw320dp folder for 7" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
Next, let's say you want your layouts for a 10" device, create values-sw720dp folder for 10" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
PS: For landscape orientation, add -land in the end of folder names.
Next, let's say you have new devices such as xhdpi and xxhdpi devices, you can create values-xhdpi and values-xxhdpi folders.
Since you have defined dimensions in values folder, when you run the App, most of the dimensions will be used from here only. And you can add/update the dimensions in respective folders for screen-size and resolutions on which there is any mismatch.
Also, if you have got separate drawables for various size like mdpi, hdpi, etc you can place them under drawable-mdpi, drawable-hdpi, etc.
The Android system will take care of loading correct resources at run-time.
Note: On Android versions < 3.2, the folder naming was little different, you can refer that here.
Hope this helps.
Android automatically scales layouts to fit the current device. One way to make the solution more elegant would be to create separate folders drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi to hold the drawable resources - this is to make sure the same resource does not get used across all the different resolutions where it might not be so clear.
Do not use px (pixels) for measurements in the layout file - use dip (or density independent pixels) which allows to scale according to device pixel density.
The other extreme would be to have different layouts for different resolutions though this is is not advisable.
On this page in table 3 at the bottom it says there are small, normal, large and extra large screen for each density (ldpi, mdpi, hdpi, xhdpi): http://developer.android.com/guide/practices/screens_support.html#support
For example, if I create mdpi normal screen (320x480) and mdpi large screen (600x1024) should I put all images for both resolutions in mdpi folder? If yes, how should I name those images because those are the same images with different sizes?
should I put all images for both resolutions in mdpi folder?
Yes. The size of the images should be the same among all screen sizes, they should vary only by density.
In this case you should use drawable-mdpi and treat your layout to better use the extra space you may have in larger screens.
If you want to give images specifically for large screen mdpi then you can create a new folder drawable-large-mdpi.
Check out : http://developer.android.com/guide/practices/screens_support.html#qualifiers
What ever the screen size and resolution, same images apply for similar dpi screens. Android will automatically resize your images to fit in the screen as you defined regardless of the size and resolution but on basis of dpi.
That means you have to put same images for all same dpi layouts.
From the same link:
To use a configuration qualifier:
Create a new directory in your project's res/ directory and name it using the format: -
is the standard resource name (such as drawable or layout).
is a configuration qualifier from table 1, below, specifying the screen configuration for which these resources are to be used (such as hdpi or xlarge).
You can use more than one at a time—simply separate each qualifier with a dash.
Save the appropriate configuration-specific resources in this new directory. The resource files must be named exactly the same as the default resource files.
Search for "Using configuration qualifiers"
i have an application that am trying to implement it on a tablet 7'. this that i have done is the follow. on the folder drawable and layout are my defaults for the normal smartphone screen. also i have create the layout-xlarge and drawable-mdpi in which on the first one i have change the sizes and on the second one i add the images with different size. my problem is that the default get the size of images that i have on the drawable-mdpi folder and not from the default drawable folder. what am i doing wrong? also i have nothing declare on the manifest.
mdpi is the default, so drawable and drawable-mdpi are the same thing and I don't know which Android chooses in this case - but you're designing for a tablet and tablets are generally mdpi devices so it correctly gets it from drawable-mdpi. You could use drawable-xlarge-mdpi if you want separate mdpi resources to be used for extra-large screens. Incidentally, remember a 7" 1024x600 tablet is large, not xlarge - so try drawable-large-mdpi and layout-large-mdpi and see if that gives you what you want.