Do you know the thing I hate the most about Android? It’s screen fragmentation! On the one hand, having a huge variety of screen sizes is great for the end users. But it’s a complete nightmare for developers! Thus, it’s really hard to create a good looking UI without any problems. There will always be a device with some wired screen size on which your layout will look bad!
There are several different approaches for handling this problem. Sadly, you can’t be 100% sure that there won’t be any UI issues. And if you have to manipulate a View through your Java code, things can get really nasty!
A good practice is to determine the current screen density of the device. We can do that with the following code snippet:
DisplayMetrics metrics = getResources().getDisplayMetrics();float density = metrics.density;
How is that useful?
My task was to create translation animations for some views. I had a xhdpi phone. Thus, I implemented the UI with values which made the views look great on this particular phone. Of course, they looked terrible on other devices. But that was not a problem.
For example, I knew that I had to translate a view with 200 dp on the X axis to look OK on my current phone. I knew my phone had a xhdpi screen. Thus, to make the UI relatively good for the rest of the devices out there, I had to find the base value for mdpi screens. I used the code from above and divided these 200 dp by the value in the density variable. And found out that the baseline value for mdpi screens was 100 dp.
Feel free to share, comment & give your opinion on the topic!
And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1