How does the use of DPI Layouts and Redeemers work?

Asked

Viewed 636 times

2

In the Android there are so-called "DPI", who are pixel densities. Each screen has its kind of DPI as an example:

Smartphones:

  • layout [?]
  • layout-ldpi [240x320 & 240x400]
  • layout-mdpi [320x480 & 480x854]
  • layout-hdpi [480x800 & 480x854]
  • layout-xhdpi [720x1280]
  • layout-xxhdpi [1080x1920]
  • layout-xxxhdpi [?]

Tablets:

  • layout-mdpi [600x1024 & 800x1280]
  • layout-xhdpi [2560x1800 | 2560x1600 | 2048x1536 & 1200x1920]
  • layout-tvhdpi [800x1280]

To each type of screen exists "folders" with picture sizes different:

  • drawable
  • drawable-hdpi
  • drawable-large
  • drawable-ldpi
  • drawable-mdpi
  • drawable-xhdpi
  • drawable-xxhdpi
  • drawable-xxxhdpi

My doubts are as follows:

1) In each layout folder, I create files with the same name as . xml to the screen. However how do I know that the android is using the screen in that particular DPI? It’s some code I have to insert to he use the layouts?

2) What is the use of the folder "layout" and "drawable" with the name "pure", without using for example "layout-ldpi"? In this same question, what are the sizes of the images in the folder "pure drawable" and "pure layout"?

3) Assuming I have to create a background, Landscape and Portrait at 800x600 resolution. The Portrait is 800x600 and of course the Landscape is 600x800. Should I create the background in these two resolutions? And how these images will be called correctly by Android?

4) Following the reasoning of the question (3), for each type of resolution, as I know what is the ideal px size for images like icons, "normal" images and others?

5) If I want to create specific "layouts" as examples for the type of dpi "sw600dp". What should I do?

6) Following the reasoning of the question (5), it is really necessary to create these types of custom "layouts", or only dpi that I mentioned above already cane?

7) How does creating layouts for tablets work? And how I make the "merge" of layouts" of tablets with those of the "smartphones" within the same Android project?

8) There are also folders of type "values", "values-v21", "values-V22". What are these folders and what should be inserted in each of them?

I thank you for your patience, and kindly follow the order of the questions so there is no confusion.

1 answer

2


1) Android knows the device resolution and automatically chooses the folder (if any created) that has the closest DPI resolution of the detected resolution. If there is no folder with the resolution defined in the name, it takes the normal folder even, in the case of layouts, the folder "layout".

2) As for the pure name, means that it is the default folder, IE, Android will query that folder only if there are the corresponding folders with qualifiers that apply, as the "layout-ldpi" you quoted, if the phone is LDPI and that folder exists, He will use her files first, otherwise he will use the pure folder. If you are not creating images for different resolutions (and save in the respective folders), save in "drawable" the image with the highest possible resolution.

3) By the same logic as the above answers. You create a "drawable-land" folder and save the images in Landscape with the same name as the equivalent images for Portrait that will be simply in "drawable". At runtime Android will get the image of the right folder, because it will know what orientation the device will be and will use the same principle of the above answer, try to match the current scenario that the app meets with the name of the folder that would best suit the scenario.

4) You have to set what size in DP’s the image will occupy in the layout, then convert this value to pixels when generating the final images for each resolution (XXHDPI, LDPI, etc). Do a quick search on Google, has several websites that show the calculation formula or even already convert and generate images for the various resolutions in DP that the app will work.

5) Same logic as always, creates a folder "layout-sw600dp" and creates an XML with the same name of the file that will run at default resolution, which is in the folder "layout".

6) Creating custom layouts for multiple resolutions will always give a better user experience, as you control how your app will be displayed in various screen sizes. Follow some good practices on this site: https://material.io/guidelines/

7) Answered in 5, but depending on the difference of components between a mobile layout and tablet for the same Activity, you will need to treat this in the code of Activity.

8) The "values" folder serves to define and organize data that is very likely to be reused in various parts of the app, so it will be easier to maintain it because it is isolated from code, for example, Strings (in strings.xml), Themes for Views (in Styles.xml), dimensions (in dimens.xml) and so on. These numeric quantifiers are to specify that the folder should be used for a particular version of the API or higher and can be used in any resource folder other than "values", such as "layout", "drawable"etc. For example, if you want a given integer value to be X in API’s 21 (Lollipop) or higher, put it in the "values-v21" folder, otherwise the value in the pure "value" folder will be used. The same goes for a layout, for example, if you want to use some component that only runs from Nougat, then you would create a layout in "layout-v23".

Here are good explanations about all this:

https://developer.android.com/guide/practices/screens_support.html?hl=pt-br https://android-developers.googleblog.com/2011/07/new-tools-for-managing-screen-sizes.html

I also recommend taking a look at this course. I happened to do it this week (it’s very short) and answers all your questions.

https://www.udacity.com/course/material-design-for-android-developers--ud862

This one’s excellent too:

https://www.udacity.com/course/android-basics-user-interface--ud834

Both are official Google courses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.