6

I've 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 Low cost device, Android 2.3.3

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

If I use different layout for each screen size, both devices says they are large-long and mdpi, so I can't distinguish them by using layout folder names... The problem is:

I use a TextView with textSize="20dp"

In the firse device, text width is half of the screen, in the second device is bigger (80% of the screen width). Why? I expect that both devices display text in the same way if I use dp (and not px). I tried also with sp but nothing changes...

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

I would try to use layout folder names like "layout-w600dp" or something else introduced in Android 3.2 but this is not the case.

I know I can change element dimensions by code in onLayout() but I don't want to do that...

Any suggestion?

Update

I solved my problems with layouts using themes: Activate a specific dimens.xml at runtime

Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • I am having the exact problem with 2 similar hardwares. Did styles solve the problem for text size and images ?how do you know which layout to pick if both devices cant be destinguished – Snake Aug 14 '12 at 06:37
  • How can you say that the 2 devices are not distinguishable? Have you tried looking at density and resolution? Are DisplayMetrics both the same? – Seraphim's Aug 16 '12 at 14:16
  • I have both devices showing display metrics as you showed it. Both similar with a small difference in xdpi,ydpi. When I said they are the same I meant they are both medium desnity large screen size devices. Yet the image sizes/button size is different – Snake Aug 16 '12 at 20:19
  • That little difference is enough to distinguish each device type. I tried to solve this problem in different ways and the most reliable is to write some "if()" and activate a specific theme at run time. I'm running on Android 2.2 as base so I can't use layout tricks introduced in Android 3.2 as described here http://developer.android.com/guide/practices/screens_support.html (If you find my question usefull please vote for it) – Seraphim's Aug 17 '12 at 13:03

1 Answers1

3

The android documentation on Dimention say this

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

So it's totally ok what you see

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

Anirudha
  • 32,393
  • 7
  • 68
  • 89
logcat
  • 3,435
  • 1
  • 29
  • 44
  • 1
    In my case density seems to be the same for both device. My need is to display a text exactly in the same way on both device, adjusting a layout weight is a solution but not so usefull for this case: if I use a layout with 50% of screen width, in the first device text is displayed in one line, in the second device text is displayed in 2 lines... – Seraphim's Jun 06 '12 at 09:44