54

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?

Reno
  • 33,594
  • 11
  • 89
  • 102
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • you can find some help from here http://stackoverflow.com/questions/16706076/font-size-and-images-for-different-devices/16910589#16910589 – Bhavesh Jethani Jul 16 '14 at 06:54
  • possible duplicate of [multiple screen support in android](http://stackoverflow.com/questions/7453982/multiple-screen-support-in-android) – arpit Aug 06 '15 at 06:03

9 Answers9

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.

Community
  • 1
  • 1
Uttam
  • 12,361
  • 3
  • 33
  • 30
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
Schwesi
  • 4,753
  • 8
  • 37
  • 62
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.

Elhanan Mishraky
  • 2,736
  • 24
  • 26
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

David.C
  • 257
  • 3
  • 17
Trinadh Koya
  • 1,089
  • 15
  • 19
  • 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 '17 at 14:48
  • 1
    Better way is to store some values like margin or text size in those folders and have one layout file – Michał Powłoka May 10 '21 at 08:54
5

Use sdp library which is provided in Github

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
Deep Adhia
  • 382
  • 4
  • 11
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.

Sathish
  • 1,455
  • 1
  • 16
  • 22
  • 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 '15 at 13:12
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"/>
ud_an
  • 4,939
  • 4
  • 27
  • 43
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.

Shafi
  • 1,368
  • 10
  • 24
  • 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 '11 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 01 '11 at 13:06
  • More info here:https://developer.android.com/training/multiscreen/screensizes.html – Shafi Dec 20 '11 at 04:59
  • using different folders for layouts like layout-small is deprecated from 3.2 – Shirish Herwade Feb 12 '13 at 09:58
  • @Ricky I have the same problem...What did you do to fix it? – Ruchir Baronia Dec 01 '15 at 04:06
  • How to manage text size in these situations? – lubrum Apr 30 '21 at 13:39
  • If it is recommended by google, how can you say it is not the best practice? – MRamzan May 23 '21 at 07:00
-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/