Different font sizes for different screen sizes

In my application I must use a smaller font for the medium density devices. Is it possible to specify that?

  • Android Studio 1.3 SVG to png conversion
  • Check for access to notifications using NotificationListenerService
  • mpandroidchart - How can I avoid the repeated values in Y-Axis?
  • How to keep the toolbar fixed at the top when AppBar collapses/expands?
  • SVG support on smart-phone browsers
  • How to use the Firebase server timestamp to generate date created?
  • How to make 4x1 size widget on Android homescreen?
  • Android Studio: Ice Cream Sandwich (API Level 15) Device Configuration not showing up even though system image is installed
  • Add (open/close) button to activity for (opening/closing) the navigation drawer
  • Get new position of coordinate after rotation with Matrix
  • SwipeRefreshLayout disable drag animation on swipe down
  • Build Unity project with Jenkins failed
  • 3 Solutions collect form web for “Different font sizes for different screen sizes”

    You should use styles, then you can have separate folders “values” (default) “values-hdpi” (high density) “values-mdpi” (medium density) and so on and put your style file with correct textSize values in each folder as needed.

    Then, when you are in medium density device it will pick the file in “values-mdpi” folder if exists or in “values” if not, and the same for high density etc…

    This same principle applies to al “res” subfolders (drawables, values, etc…)

    Specify all your fonts using dips (e.g. 14dp) rather than pixels (e.g. 14px) and you won’t need to worry about screen density. Android will scale your fonts (and layout) accordingly.

    Edit: Here’s comparison of sp/dp from the Android docs:

    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 (dots per inch) screen, so 160dp is always one inch regardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens.

    sp
    Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user’s preference.

    yes, for implementing an universal app which will be working for all resolution.
    You should configure multiple drawables & corresponding layouts.

    eg.

    drawables:-

    drawable-ldpi
    drawable-mdpi
    drawable-hdpi

    layouts:-

    layout-small
    layout-medium
    layout-large

    then you can change according to your resolution required for Device.
    Android supports internal configuration for Density factor of various Screen’s resolution.
    the device can take itself as appropriate drawable & corresponding layout.
    you dont need to adjust any line of code in your src files.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.