1

In one of my Activities I'd like to adjust an amount of textual content according to current screen size. What metric should I rely on?

Just width in pixels seems not too reliable since font scaling can be very different. On the other hand DisplayMetrics's DENSITY_280, DENSITY_400 ... seem too broad. Is there any normalized metric available like width_in_pixels * font_scaling?

Just in case: My min SDK verion is 15 (4.0.3)

src091
  • 2,679
  • 6
  • 38
  • 70

3 Answers3

2

When specifying text size, always use sp:

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp" />

more details here and here

Community
  • 1
  • 1
hornet2319
  • 379
  • 3
  • 15
1

You can check this link to know how to Supporting Multiple Screens on Android.

The secret is using dp units on your layouts.

  • px is one pixel.
  • sp is scale-independent pixels.
  • dp is Density-independent pixels.

dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

João Marcos
  • 3,763
  • 1
  • 18
  • 14
1

you can get values like dpi from DisblayMetrics , look here docs

also, there is a built-in converter for different size metrics in api, TypedValue.applyDimension() , which can be used to get pixels from sp or dp

Raiv
  • 5,561
  • 1
  • 32
  • 51