How to define dimens.xml for every different screen size in android?
Asked Answered
S

10

163

When supporting different screen sizes (densities) in Android often the focus is on creating different layouts for every possible screen. I.E.

  • ldpi
  • mdpi
  • hdpi
  • xhdpi
  • xxhdpi
  • xxxhdpi

I designed a layout for an xhdpi screen as a reference, and defined it's dimensions in dimens.xml. Now I want to give support it to every possible screen size. How can I do that?

As far as I know, I can use this tool to figure out the proper dimens.xml for other screen sizes and add it to my project. Is this the right way to do it in my situation?

Another question, do I only need to create dimens.xml for above screen dimensions? If yes then what is w820dp?

Thanks for your help. I need to support phones only (not tablets or other devices).

Shopwindow answered 30/9, 2015 at 7:57 Comment(3)
Yes you have to create a different dimesn.xml files for different resolutions.Royce
@PiyushGupta That will for all above dimensional screen? But what is W820dp is it something supporting different android version? Also can you suggest me any tutorial on supporting different screen on the basis of dimens.xml?Shopwindow
It is for tablet or for others. Otherwise as i said you need to create a different values folder.Royce
A
317

You have to create Different values folder for different screens . Like

values-sw720dp          10.1” tablet 1280x800 mdpi

values-sw600dp          7.0”  tablet 1024x600 mdpi

values-sw480dp          5.4”  480x854 mdpi 
values-sw480dp          5.1”  480x800 mdpi 

values-xxhdpi           5.5"  1080x1920 xxhdpi
values-xxxhdpi           5.5" 1440x2560 xxxhdpi

values-xhdpi            4.7”   1280x720 xhdpi 
values-xhdpi            4.65”  720x1280 xhdpi 

values-hdpi             4.0” 480x800 hdpi
values-hdpi             3.7” 480x854 hdpi

values-mdpi             3.2” 320x480 mdpi

values-ldpi             3.4” 240x432 ldpi
values-ldpi             3.3” 240x400 ldpi
values-ldpi             2.7” 240x320 ldpi

enter image description here

For more information you may visit here

Different values folders in android

http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html

Edited By @humblerookie

You can make use of Android Studio plugin called Dimenify to auto generate dimension values for other pixel buckets based on custom scale factors. Its still in beta, be sure to notify any issues/suggestions you come across to the developer.

Anibalanica answered 30/9, 2015 at 8:21 Comment(11)
shouldn't there be "values-sw480dp-hdpi" .xhdpi and xxhdpi and so on ? only 320dp have such in your answer.. please replyDinkins
@IntelliJAmiya: You should mention the difference between Android <=3.2 and >3.2.Advantage
@IntelliJAmiya I have created a studio plugin called Dimenify. This lets you create dimensions for other buckets easily. Could you add an edit with the same so others can make use of it? plugins.jetbrains.com/androidstudio/plugin/9349-dimenifyPerpend
@Perpend glad to hear that .You can edit my answerAnibalanica
@Perpend Move aheadAnibalanica
is there's a way to create layout for specific dimension/density ?Efta
@ralphspoon I did not try thisAnibalanica
@IntelliJAmiya for sure, there will be new releases that will not be covered on those folders and/or may not be appropriate on certain folder.Efta
What about landscape mode ?Cady
@IntelliJAmiya How to support for android devices with aspect ratio 18:5:9. E.g: Samsung galaxy s8. The above different values folder doesn't for such devices.Cady
is there a big difference if we only create values-sw320dp, values-sw600dp...et cetera instead of values-sw320dp-xhdpi, values-sw320dp-hdpi, values-sw320dp-mdpi? Is that suffix mdpi/hdpi/xhdpi at the end of values-swXXXdp that important?Greatuncle
F
57

Use Scalable DP

Although making a different layout for different screen sizes is theoretically a good idea, it can get very difficult to accommodate for all screen dimensions, and pixel densities. Having over 20+ different dimens.xml files as suggested in the above answers, is not easy to manage at all.

How To Use:

To use sdp:

  1. Include implementation 'com.intuit.sdp:sdp-android:1.0.5' in your build.gradle,
  2. Replace any dp value such as 50dp with a @dimen/50_sdp like so:

    <TextView
     android:layout_width="@dimen/_50sdp"
     android:layout_height="@dimen/_50sdp"
     android:text="Hello World!" />
    

How It Works:

sdp scales with the screen size because it is essentially a huge list of different dimens.xml for every possible dp value.

enter image description here

See It In Action:

Here it is on three devices with widely differing screen dimensions, and densities:

enter image description here

Note that the sdp size unit calculation includes some approximation due to some performance and usability constraints.

Francophobe answered 6/8, 2018 at 13:48 Comment(8)
Much obliged. This was really useful for me.Handgun
@Advait can you please tell me which values folder should i use for Samsung Edge 7 with 5.5 inch mobile with 2560x1440 resolution ?Malapropos
@Advait currently i am using values-xxhdpi folder but problem is, it is taking same folder for 5, 5.5 & 6 screens same folder so there is scroll appears on 5 inch screens & 5.5. I don't want that. can you please help me in this ?Malapropos
@Advait S values-xxhdpi for 5.5" 1080x1920 & Also for 5.5" 1440x2560 I want different folder for 5.5" 1440x2560.Malapropos
@ULHASPATIL exactly! I bumped with similar problem and don't understand how to resolve it. Did you find any solution for screen with similar inches and different pixel resolution?Steinberg
Not working for all devices size will change when i open in to tabletWethington
it is not clear how to convert negative values, in my case <item name="android:dropDownVerticalOffset">-40dp</item>Salutation
@AndrewGlukhoff you can try negative values up to negative 60 scalable-dp like so: @dimen/_mins60sdpFrancophobe
T
28

we want to see the changes of required view size in different screens.

We need to create a different values folders for different screens and put dimens.xml file based on screen densities.

I have taken one TextView and observed the changes when i changed dimens.xml in different values folders.

Please follow the process

normal - xhdpi \ dimens.xml

The below devices can change the sizes of screens when we change the normal - xhdpi \ dimens.xml

nexus 5X ( 5.2" * 1080 * 1920 : 420dpi )

nexus 6P ( 5.7" * 1440 * 2560 : 560dpi)

nexus 6 ( 6.0" * 1440 * 2560 : 560dpi)

nexus 5 (5.0", 1080 1920 : xxhdpi)

nexus 4 (4.7", 768 * 1280 : xhdpi)

Galaxy nexus (4.7", 720 * 1280 : xhdpi)

4.65" 720p ( 720 * 1280 : xhdpi )

4.7" WXGA ( 1280 * 720 : Xhdpi )

Xlarge - xhdpi \ dimens.xml

The below devices can change the sizes of screens when we change the Xlarge - xhdpi \ dimens.xml

nexus 9 ( 8.9", 2048 * 1556 : xhdpi)

nexus 10 (10.1", 2560 * 1600 : xhdpi)

large - xhdpi \ dimens.xml

The below devices can change the sizes of screens when we change the large - xhdpi \ dimens.xml

nexus 7 ( 7.0", 1200 * 1920: xhdpi)

nexus 7 (2012) (7.0", 800 * 1280 : tvdpi)

The below screens are visible in " Search Generic Phones and Tablets "

large - mdpi \ dimens.xml

The below devices can change the sizes of screens when we change the large - mdpi \ dimens.xml

5.1" WVGA ( 480 * 800 : mdpi )

5.4" FWVGA ( 480 * 854 : mdpi )

7.0" WSVGA (Tablet) ( 1024 * 600 : mdpi )

normal - hdpi \ dimens.xml

The below devices can change the sizes of screens when we change the normal - hdpi \ dimens.xml

nexus s ( 4.0", 480 * 800 : hdpi )

nexus one ( 3.7", 480 * 800: hdpi)

small - ldpi \ dimens.xml

The below devices can change the sizes of screens when we change the small - ldpi \ dimens.xml

2.7" QVGA Slider ( 240 * 320 : ldpi )

2.7" QVGA ( 240 * 320 : ldpi )

xlarge - mdpi \ dimens.xml

The below devices can change the sizes of screens when we change the xlarge - mdpi \ dimens.xml

10.1" WXGA ( tABLET) ( 1280 * 800 : MDPI )

normal - ldpi \ dimens.xml

The below devices can change the sizes of screens when we change the normal - ldpi \ dimens.xml

3.3" WQVGA ( 240 * 400 : LDPI )

3.4" WQVGA ( 240 * 432 : LDPI )

normal - hdpi \ dimens.xml

The below devices can change the sizes of screens when we change the normal - hdpi \ dimens.xml

4.0" WVGA ( 480 * 800 : hdpi )

3.7" WVGA ( 480 * 800 : hdpi )

3.7" FWVGA Slider ( 480 * 854 : hdpi )

normal - mdpi \ dimens.xml

The below devices can change the sizes of screens when we change the normal - mdpi \ dimens.xml

3.2" HVGA Slider ( ADP1 ) ( 320 * 480 : MDPI )

3.2" QVGA ( ADP2 ) ( 320 * 480 : MDPI )

Thalassography answered 17/8, 2016 at 8:16 Comment(3)
So according to you the folders would then be named something like values-normal-xhdpi?Paperhanger
Yes, i agree with you, instead of write values- to all the files, i said, "create different values folders for different text sizes". So please read the title. Thanks.Thalassography
it doesn't work #67604835 it always takes values from the same folder for all devicesGallup
R
13

There are nice libraries which will handle everything and reduce your pain. For using it, just add two dependencies in gradle:

 implementation 'com.intuit.ssp:ssp-android:1.0.5'
 implementation 'com.intuit.sdp:sdp-android:1.0.5'

After that, use dimens like this:

        android:layout_marginTop="@dimen/_80sdp"
Recycle answered 7/3, 2019 at 14:24 Comment(3)
Here are the libraries for reference: SDP and SSP. And thanks for this precise answer!Drumm
This is also not perfect according to current time 2020Caracara
if you consider the ideal size as 5". it is fine to use these libraries.Roslynrosmarin
R
8

You have to create a different values folder for different screens and put dimens.xml file according to densities.

1) values

2) values-hdpi (320x480 ,480x800)

3) values-large-hdpi (600x1024)

4) values-xlarge (720x1280 ,768x1280 ,800x1280 ,Nexus7 ,Nexus10)

5) values-sw480dp (5.1' WVGA screen)

6) values-xhdpi (Nexus4 , Galaxy Nexus)
Royce answered 30/9, 2015 at 8:15 Comment(6)
Where can I get the ration in between screen size? As I can see in the tool they defined everything in ldpi,mdpi mannerShopwindow
That will depends on your values folder which will be retreive values at most from it.Royce
Do I need to mention values-xxhdpi for Nexus 6?Shopwindow
320x480 is typical mdpi. So, put it either in values or in values-mdpiIllusionism
What about landscape mode ?Cady
it doesn't work #67604835 it always takes values from the same folder for all devicesGallup
U
8

I've uploaded a simple java program which takes your project location and the dimension file you want as input. Based on that, it would output the corresponding dimension file in the console. Here's the link to it:

https://github.com/akeshwar/Dimens-for-different-screens-in-Android/blob/master/Main.java

Here's the full code for the reference:

public class Main {


    /**
     * You can change your factors here. The current factors are in accordance with the official documentation.
     */
    private static final double LDPI_FACTOR = 0.375;
    private static final double MDPI_FACTOR = 0.5;
    private static final double HDPI_FACTOR = 0.75;
    private static final double XHDPI_FACTOR = 1.0;
    private static final double XXHDPI_FACTOR = 1.5;
    private static final double XXXHDPI_FACTOR = 2.0;

    private static double factor;

    public static void main(String[] args) throws IOException {


        Scanner in = new Scanner(System.in);
        System.out.println("Enter the location of the project/module");
        String projectPath = in.nextLine();

        System.out.println("Which of the following dimension file do you want?\n1. ldpi \n2. mdpi \n3. hdpi \n4. xhdpi \n5. xxhdpi \n6. xxxhdpi");

        int dimenType = in.nextInt();

        switch (dimenType) {
            case 1: factor = LDPI_FACTOR;
                break;
            case 2: factor = MDPI_FACTOR;
                break;
            case 3: factor = HDPI_FACTOR;
                break;
            case 4: factor = XHDPI_FACTOR;
                break;
            case 5: factor = XXHDPI_FACTOR;
                break;
            case 6: factor = XXXHDPI_FACTOR;
                break;
            default:
                factor = 1.0;
        }

        //full path = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/src/main/res/values/dimens.xml"
        //location of the project or module = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/"


        /**
         * In case there is some I/O exception with the file, you can directly copy-paste the full path to the file here:
         */
        String fullPath = projectPath + "/src/main/res/values/dimens.xml";

        FileInputStream fstream = new FileInputStream(fullPath);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;

        while ((strLine = br.readLine()) != null)   {
            modifyLine(strLine);
        }
        br.close();

    }

    private static void modifyLine(String line) {

        /**
         * Well, this is how I'm detecting if the line has some dimension value or not.
         */
        if(line.contains("p</")) {
            int endIndex = line.indexOf("p</");

            //since indexOf returns the first instance of the occurring string. And, the actual dimension would follow after the first ">" in the screen
            int begIndex = line.indexOf(">");

            String prefix = line.substring(0, begIndex+1);
            String root = line.substring(begIndex+1, endIndex-1);
            String suffix = line.substring(endIndex-1,line.length());


            /**
             * Now, we have the root. We can use it to create different dimensions. Root is simply the dimension number.
             */

            double dimens = Double.parseDouble(root);
            dimens = dimens*factor*1000;
            dimens = (double)((int)dimens);
            dimens = dimens/1000;
            root = dimens + "";

            System.out.println(prefix + " " +  root + " " + suffix );

        }

        System.out.println(line);
    }
}
Ustulation answered 25/5, 2016 at 7:7 Comment(0)
S
6

You can put dimens.xml in

1) values

2) values-hdpi

3) values-xhdpi

4) values-xxhdpi

And give different sizes in dimens.xml within corresponding folders according to densities.

Sporocyst answered 30/9, 2015 at 8:7 Comment(2)
According do Android documentation small/normal/large are deprecated.Portentous
it doesn't work #67604835 it always takes values from the same folder for all devicesGallup
V
2

The basic principle of getting more flexibility in your code is by encapsulating what is varying/changing (or what is highly likely to change).

If your case dimensions are changing. So you start with moving your dimensions (the ones you think need to change) from XML code or from java/kotlin code to a file values/dimens.xml

For the context of this question. You may have res folders by density qualifiers or by small-width qualifiers.

values/dimens.xml
values-ldpi/dimens.xml
values-hdpi/dimens.xml

values/dimens.xml
values-sw320dp/dimens.xml
values-sw320dp/dimens.xml
 

One important point to note here the items in values/dimens.xml will apply to screens smaller than the lowest breakpoint you have specified.

values/dimens.xml //smallest screen
values-sw320dp/dimens.xml //320dp or more till next breakpoint folder
values-sw360dp/dimens.xml//360dp or more till next breakpoint folder

Another important point to note is that a high pixel density device may have a small screen. So be sure whether you want to specify density qualifier or screen width qualifier.


Some further briefing for those who are new to this aspect.

You can add qualifiers to different resource folders such as values, drawable, and layout.

These qualifiers may represent a language (-en,-hi) ,an orientation (-land,-port), or screen size range (-ldpi,-hdpi).

The naming convention has a hierarchy of qualifiers and qualifiers must be added in that order only.

    values-en-hdpi (it cannot be values-hdpi-en)
    values-en-land-hdpi (it cannot be values values-hdpi-en-land or any other order)
layout-hdpi

The order is documented in Table 2 at this page

Some qualifiers represent absolute values whereas others like ldpi,hdpi represent breakpoints.

Verbalize answered 16/6, 2022 at 11:7 Comment(0)
P
1

In case you want to view more: Here's a link for a list of devices (tablet, mobile, watches), including watch,chromebook, windows and mac. Here you can find the density, dimensions, and etc. Just based it in here, it's a good resource if your using an emulator too.

If you click a specific item, it will show more details in the right side. sample

Since it's Android , I will post related to it. sample1

sample2

~ It's better if you save a copy of the web. To view it offline.

Predominance answered 6/10, 2016 at 5:47 Comment(0)
A
1

Android 3.2 introduces a new approach to screen sizes,the numbers describing the screen size are all in “dp” units.Mostly we can use

smallest width dp: the smallest width available for application layout in “dp” units; this is the smallest width dp that you will ever encounter in any rotation of the display.

To create one right click on res >>> new >>> Android resource directory

From Available qualifiers window move Smallest Screen Width to Chosen qualifiers

In Screen width window just write the "dp" value starting from you would like Android Studio to use that dimens.

Than change to Project view,right click on your new created resource directory

new >>> Values resource file enter a new file name dimens.xml and you are done.

Associate answered 23/12, 2017 at 13:34 Comment(1)
Thanks for explaining on how to actually create these folders.Leadbelly

© 2022 - 2024 — McMap. All rights reserved.