Custom dimens.xml for mdpi devices - how it can be done? - android

My application have layout, which looks perfect at all devices, except devices with mdpi screen destiny. Activity just doesn't fit to screen on mdpi devices.
So I want to create special dimens.xml for these type of devices.
I created "values-mdpi" folder next to my "values" folder, created new dimens.xml into it and set dimens values for mdpi devices.
res/values/dimens.xml:
<resources>
<dimen name="logo_block_height">100dp</dimen>
</resources>
res/values-mdpi/dimens.xml:
<resources>
<dimen name="logo_block_height">50dp</dimen>
</resources>
And ImageView that uses that dimen:
<ImageView
android:layout_width="250dp"
android:layout_height="#dimen/logo_block_height"
android:id="#+id/font_logo" android:adjustViewBounds="false" android:src="#drawable/bg_font_logo"
android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/>
The problem is that values now applied for all devices, not only for mdpi, but for hdpi, xhdpi, xxhdpi too.
I want it to work like that:
If device screen destiny is mdpi or lower - use values from values-mdpi folder
If device screen destiny is MORE than mdpi - use default "values" folder.
How it can be done?

So I came up with only one solution for that without creating values folder for each screen density.
When at least one "values" folder with destiny qualifiers created android selects the most suitable folder for device screen destiny.
This is the reason why it selects "values-mdpi" for everything that bigger or smaller than mdpi.
Also, like ataulm said, there are no sense use screen density qualifiers in that situations. I can have phone with 340x480 with 160dpi where content doesn't fit and Tablet with 1280x800 with 160dpi where there is too much free space.
I solved that by creating Values folder with minimal-height qualifiers.
I have folder "values-h480dp" with values for every device with minimum 480 dp availible height and put default values in it. In default "values" folder i put values for devices with small screen.
Now it works like I need:
If device have availible height more than 480dp - it uses values from "values-h480dp" folder.
If device have availible height less than 480dp - it uses values from "values" folder.
I sorry for my bad English and hope that this has helped someone.

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.

How to set font for diffrent devices

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

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

The process of creating Android layout files

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.

Force tablet to use xhdpi resources

I'm working on an application and i've provided all the resources from ldpi to xhdpi, but some tablets use hdpi (or tvdpi in the case of the nexus 7 i'm testing on) so when i open the app on a tablet everything is small, i would like to force them to use the xhdpi resources how can i do that?
I don't want to create separate files just for the tablets or the size of the app would increase, the xhdpi resources would work fine
On the phones everything is displaying properly, the tablets are the problem because they take small resolution assets for their big screens, so the actual size in inch( 4cm, measured with a ruler) is correct, but they actually look small making the overall design incorrect
Usually, downscaling is the best option (as opposed to up-scaling).
If your images don't degrade or loose information when they are downscaled, put the 2x version of your images in the res/drawable-xhdpi folder. Android will scale these images down appropriately on lower density devices.
E.g. if you put a 64x64 pixel jpg/png in res/drawable-xhdpi, Android will show it as a 32x32 pixel on mdpi devices, as a 48x48 pixel image on hdpi devices and as a 64x64 pixel image on xhdpi devices.
If downscaling your images would deteriorate your them (e.g. you have 1 pixel lines in your image that would disappear when scaled down), then the only option is to add the variations of your images in the drawable-mdpi, drawable-hdpi, etc. directories.
Update after comments:
You'd want to have your images appear larger on larger screens (not on higher density screens). You could do this by still putting your images in the drawable-xhdpi folder only, but then do this:
E.g. you'll have this layout in res/layout with an ImageView that shows one of your images:
res/drawable-xhdpi/one_of_my_images.png
res/layout/somelayout.xml
....
<ImageView
android:layout_height="#dimen/image_height"
android:layout_width="#dimen/image_width"
android:src="#drawable/one_of_my_images"
android:scaleType="fitCenter"
....
/>
res/values/dimens.xml
....
<dimen name="image_height">100dip</dimen>
<dimen name="image_width">150dip</dimen>
....
res/values-large/dimens.xml
....
<dimen name="image_height">150dip</dimen>
<dimen name="image_width">225dip</dimen>
....
res/values-xlarge/dimens.xml
....
<dimen name="image_height">200dip</dimen>
<dimen name="image_width">300dip</dimen>
....
As you can see, the larger the screen, the larger (in dip and therefore in inches) the image.
You can experiment with different values for image_height and image_width in dimens.xml in res/values-xlarge-hdpi, res/values-xlarge-xhdpi, etc and see what looks best.
Update 2 after comments:
If you want your own 'custom' scheme of loading the appropriate resource, put them id res/drawable-nodpi:
E.g
res/drawable-nodpi/one_of_my_images_normal.png
res/drawable-nodpi/one_of_my_images_large.png
res/drawable-nodpi/one_of_my_images_xlarge.png
Then put in the appropriate drawable dir (res/drawable/, res/drawable-large, /res/drawable-sw360dp/, etc, whatever you can come up with) an xml file that indirectly references the correct resource from the nodpi directory:E.g.
res/drawable-xlarge-mdpi/my_images.xml
<resources>
....
<item type="drawable" name="one_of_my_images">#drawable/one_of_my_images_large</item>
....
</resources>
This means that for extra-large mdpi screens, Android will load the 'one_of_my_image_large.png' from the drawable-nodpi directory.

Categories

Resources