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).
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
:implementation 'com.intuit.sdp:sdp-android:1.0.5'
in yourbuild.gradle
,Replace any
dp
value such as50dp
with a@dimen/50_sdp
like so:How It Works:
sdp
scales with the screen size because it is essentially a huge list of differentdimens.xml
for every possibledp
value.See It In Action:
Here it is on three devices with widely differing screen dimensions, and densities:
Note that the
sdp
size unit calculation includes some approximation due to some performance and usability constraints.You have to create a different
values
folder for different screens and putdimens.xml
file according to densities.