55

I'm developing an app in android and I have to support all different screen sizes and density. So i've created different folder for layout : layout-small layout-large and layout.

Then I've created different folder for image: ldpi, mdpi and hdpi. In all drawable folder the image must be with different size true? I ask this cause of I have a phone with screen size large and density medium, the image shown will be smaller and they will not take the right size?

2

9 Answers 9

85

For Different screen size, 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 small, 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

The following code in the Manifest supports all dpis.

<supports-screens android:smallScreens="true" 
          android:normalScreens="true" 
          android:largeScreens="true"
          android:xlargeScreens="true"
          android:anyDensity="true" />

And also check out my SO answer.

7
  • 20
    using different folders for layouts like layout-small is deprecated from 3.2 Feb 12, 2013 at 9:58
  • my layouts from layout-large and layout-xlarge is not taken y
    – Vikky
    Jun 15, 2013 at 8:57
  • 1
    Should we add layout-xxlarge ? Aug 24, 2013 at 9:18
  • 1
    Is this answer is still useful in 2019.could you please reply me. Jul 14, 2019 at 10:35
  • 1
    How can we check in which device which layout is showing like small and default @Uttam Sep 7, 2019 at 7:57
19

Beginning with Android 3.2 (API level 13), size groups (folders small, normal, large, xlarge) are deprecated in favor of a new technique for managing screen sizes based on the available screen width.



There are different resource configurations that you can specify based on the space available for your layout:

1) Smallest Width - The fundamental size of a screen, as indicated by the shortest dimension of the available screen area.

Qualifier Value: sw'dp value'dp

Eg. res/sw600dp/layout.xml -> will be used for all screen sizes bigger or equal to 600dp. This does not take the device orientation into account.


2) Available Screen Width - Specifies a minimum available width in dp units at which the resources should be used.

Qualifier Value: w'dp value'dp

Eg. res/w600dp/layout.xml -> will be used for all screens, which width is greater than or equal to 600dp.


3) Available Screen Height - Specifies a minimum screen height in dp units at which the resources should be used.

Qualifier Value: h'dp value'dp

Eg. res/h600dp/layout.xml -> will be used for all screens, which height is greater than or equal to 600dp.



So at the end your folder structure might look like this:

res/layout/layout.xml -> for handsets (smaller than 600dp available width)
res/layout-sw600dp/layout.xml -> for 7” tablets (600dp wide and bigger)
res/layout-sw720dp/layout.xml -> for 10” tablets (720dp wide and bigger)


For more information please read the official documentation:
https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

3
  • the minimum here means < minimum or < = minimum?
    – Sai
    Feb 21, 2019 at 13:43
  • also, can use this method for values, like values-h400dp ??
    – Sai
    Feb 21, 2019 at 14:00
  • This does not seem to be accurate. For example, I have a layout-h400dp and Android Studio keeps selecting that one for a device that's actually 320px in height. Jan 30, 2020 at 13:27
14

You can use sdp size unit instead of dp size unit. The sdp size unit is relative to the screen size and therefor is often preferred for targeting multiple screen sizes.

Use it carefully! for example, in most cases you still need to design a different layout for tablets.

1
  • Hi is it recomannded using sdp libarary? <dimen name="my_text_size">@dimen/_18ssp</dimen>
    – Jess
    Jun 12, 2020 at 11:05
7

It sounds lofty,when it comes to supporting multiple screen Sizes.The following gves better results .

res/layout/layout-w120dp
res/layout/layout-w160dp
res/layout/layout-w240dp
res/layout/layout-w160dp
res/layout/layout-w320dp
res/layout/layout-w480dp
res/layout/layout-w600dp
res/layout/layout-w720dp

Chek the Device Width and Height using Display Metrics

Place/figure out which layout suits for the resulted width of the Device .

let smallestScreenWidthDp="assume some value(Which will be derived from Display metrics)"

All should be checked before setContentView().Otherwise you put yourself in trouble

     Configuration config = getResources().getConfiguration();


        if (config.smallestScreenWidthDp >= 600) {
            setContentView(R.layout.layout-w600dp);
        } else {
            setContentView(R.layout.main_activity);
        }

In the top,i have created so many layouts to fit multiple screens,it is all depends on you ,you may or not.You can see the play store reviews from Which API ,The Downloads are High..form that you have to proceed.

I hope it helps you lot.Few were using some third party libraries,It may be reduce your work ,but that is not best practice. Get Used to Android Best Practices.

Check This Out

2
  • I created all the above folders (120dp-720dp) and I just copied my layout in each of the folders. Is that the correct way or I should edit each different layout in each different folder?
    – Johny
    Apr 20, 2017 at 14:48
  • 1
    Better way is to store some values like margin or text size in those folders and have one layout file May 10, 2021 at 8:54
5

Use sdp library which is provided in Github

1
  • 1
    Looks like this project is not maintained anymore.
    – lubrum
    Apr 30, 2021 at 13:31
3

Android adjust by it self you can put separate image for different folder if you want to use different images for high resolution devices and other device. Otherwise just put in one drawable,layout folder only for some images you can make 9-patch also.

read here

you need permission in manifest for multiple screen support link

<supports-screens android:resizeable=["true"| "false"]
                  android:smallScreens=["true" | "false"]
                  android:normalScreens=["true" | "false"]
                  android:largeScreens=["true" | "false"]
                  android:xlargeScreens=["true" | "false"]
                  android:anyDensity=["true" | "false"]
                  android:requiresSmallestWidthDp="integer"
                  android:compatibleWidthLimitDp="integer"
                  android:largestWidthLimitDp="integer"/>
1
  • 3
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" />
    – Bob
    Nov 24, 2011 at 11:39
3

You can figure out the dimensions of the screen dynamically

Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int Height= mDisplay.getHeight();

The layout can be set using the width and the height obtained using this method.

1
  • 1
    Not even close. I once tried to explore such option... I wouldn't get 1920x1080, but depending on the orientation something like 1920x 1036 or whatever my nav bar is set to. And in landscape I'd get 1080x986 for example ... I'd love it to be that easy ....similarly to html and old style use of tables buuut .... :( Feb 26, 2015 at 13:12
0

Adding to @ud_an

It is not a good practice to create different folders for layouts. Create your layout such that it works fine with all the screen sizes. To achieve this, play with the layout attributes. You only need to have different images for hdpi, mdpi and ldpi types. The rest will be managed by android OS.

7
  • Really? An old app of mine used dp for the measurements in the layout folder and on bigger resolutions it looked stupid (tiny buttons, huge layout etc), the OS didn't handle it very well at all. The code was quite old though, so maybe I did something wrong.
    – Ricky
    Nov 24, 2011 at 11:17
  • 1
    you can handle them all in one layout without using dp if you want to minimize your coding efforts. Avoid hardcoding the layout sizes and make them dynamic wrt the content. For tiny buttons, as i said, you need different drawables. Its upto you to re code different layouts or make the layouts work on all sizes.
    – Shafi
    Dec 1, 2011 at 13:06
  • More info here:developer.android.com/training/multiscreen/screensizes.html
    – Shafi
    Dec 20, 2011 at 4:59
  • using different folders for layouts like layout-small is deprecated from 3.2 Feb 12, 2013 at 9:58
  • @Ricky I have the same problem...What did you do to fix it? Dec 1, 2015 at 4:06
-1

you can create bitmaps for the highes resolution / size your application will run and resize them in the code (at run time)

check this article http://nuvornapps-en.blogspot.com.es/

1
  • This does not answer properly the question.
    – lubrum
    Jun 6, 2021 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.