KIO
Kreative Ideen online
Screen-specific folders

Screen-specific folders

Let’s say you want to your app to use a specific layout when it’s used on a phone, and another layout when it’s running on a tablet. But how can you do this?

Put screen-specific resources in screen-specific folders

As an example, you put images intended for devices with high-density screens in the “drawable-hdpi” folder. You can do something similar with other resources such as layouts, menus, and values. If you want to create multiple versions of the same resource for different screen specs, you need to create multiple resource folders with an appropriate name, then add the sesource to that folder. The device will then load the resource at runtime from the folder that’s the closest match to its creen spec.

If you want to have one layout for alarge screen devices such as tablets, and another layout for smaller devices such as phones, you put the layout for the tablet in the app/src/main/res/layout-large folder, and the layout for the phone in the app/src/main/res/layout folder.

When the app runs on a phone, it will use the layout in the layout folder. If it run’s on a tablet, it will use the layout in the layout-large folder instead.

Android uses the names of your resource folders to decide which resources it should use at runtime. Layouts in the layout folder can be used by any device, but layouts in the layout-large folder will only be used by devices with a large screen.

You can put all kinds of resources(drawables or images, layouts, menus, and values) in different folders to specify which types of device they should be used with. The screen-specific folder name can include ..

  • screen size
  • density
  • orientation
  • aspect ratio

with each part separated by hyphens.

As an example, if you want to create a layout that will only be used by very large tablets in landscape mode, you would create a folder called “layout-xlarge-land” and put the layout file in that folder.

Here are the different options you can use for the folder names:

Resource typeScreen sizeScreen densityOrientationAspect ratio
drawable-small-1dpi-land-long
(long is for screens that have a very high value for hight)
layout-normal-mdpi-port-notlong
menu-large-hdpi
mpimap
(A mipmap resource is used for app icons)
-xlarge-xhdpi
values-xxhdpi
-xxxhdpi
-nodpi
(This is for density-independent resources. Use -nodpi for any image resource you don’t want to scale(eg, a folder called drawable-nodpi))
-tvdpi

Android decides at rundtime which resources to use by checking the spec fo the device and looking for the best match.If there’s no exact match, it will use resources designed for a smaller screen than the current one. If resources are only available for screens larger than the current one, Android won’t use them and the app will crash.

If you only want your app to work on devices with particular screen sizes, you can specify this in AndroidManifest.xml using the <supports-screens> attribute. As an example, if you don’t want your app to run on devices with small screens, you’d use: <supports-screens android:smallScreens=”false” />

For more information on the settings see:
https://developer.android.com/guide/practices/screens_support.html

Leave a Reply

Your email address will not be published. Required fields are marked *