gmatcat
gmatcat

Reputation: 185

What is the best practice for creating layouts for all screen sizes and all screen densities in Android

Based on what I read (including the official documentation), it is suggested to create separate layouts, separate drawables, separate dimens.xml files to fit all screen sizes and screen densities but from what I see, there are more than 100 different combinations of screen size/screen density of phones and creating unique layouts/drawables for each one of them is next to impossible.

The same result can be achieved by simply adjusting a single layout programmatically, however, no one suggests this approach at all. Why?

Even if I use dp in layouts in sizing the elements, it will still look different on different densities.

Let's say you have layouts for large screen devices with xhdpi density, do you make your app available to only those devices? Seems improbable.

How could someone possibly make an app that would fit all screen sizes/screen density?

There must be an optimal approach in creating layouts for all screen sizes and all screen densities but what? I'm actually REAALLY lost. Can anyone provide a simple project that somehow does this?

Reference: Is there a list of screen resolutions for all Android based phones and tablets?

Upvotes: 1

Views: 455

Answers (2)

Tuan Nguyen
Tuan Nguyen

Reputation: 2607

I have some solutions for you

  1. Use ConstraintLayout (https://developer.android.com/training/constraint-layout/)
  2. Use PercentRelativeLayout (https://developer.android.com/reference/android/support/percent/PercentRelativeLayout)
  3. Sdp converter (https://github.com/intuit/sdp)

In my case. I use all of them. They can work together as well.

Upvotes: 1

Koustuv Ganguly
Koustuv Ganguly

Reputation: 899

You can go for 2 things :

  1. Programatically adjusting the UI components height and width based on device screen height and width.
  2. In xml use weight rather than using fixed height and weight,so everything will be based on ratio.

Note: You may not use both of these together.

Upvotes: 0

Related Questions