Different sizes on two devices, even if I use "dp"

I have 2 tablets:

1 Samsung Android 3.0

DisplayMetrics {density=1.0, width=600, height=976, scaledDensity=1.0, xdpi=161.55031, ydpi=155.51021} 

1 inexpensive device, Android 2.3.3

  DisplayMetrics {density=1.0, width=480, height=800, scaledDensity=1.0, xdpi=160, ydpi=160.42105} 

If I use a different layout for each screen size, both devices say that they are large and mdpi, so I can not distinguish them using the folder names of the layout ... The problem is this:

I am using TextView with textSize = "20dp"

In the firse device, the text width is half the screen, in the second - more (80% of the screen width). What for? I expect both devices to display text in the same way if I use dp (not px). I also tried with sp, but nothing has changed ...

(I used TextView as an example, I have the same problem with all the elements in the layout: button sizes, ...)

I would try using layout folder names such as "layout-w600dp" or something else that appeared in Android 3.2, but that is not the case.

I know that I can resize an element by code in onLayout (), but I do not want to do this ...

Any suggestion?

Update

I solved my problems with layouts with the help of themes: Activate a specific diameter.xml at runtime

+6
source share
1 answer

Android documentation for Dimention say it

The dp-to-pixel ratio will change with the density of the screen, but not necessarily in direct proportion

So it's alright what you see

You can achieve 50% of the screen using LinearLayout weigth .. Refer this

+3
source

Source: https://habr.com/ru/post/917432/


All Articles