1

I'm developing an Android app on my Nexus 5 device (~445 ppi) and I'm using Roboto Light as the font for a few sections in the application. When displayed in my device, the Roboto Light text looks clean and crisp, however when testing the app on device with screens that have a lower pixel density, Roboto Light looks pretty bad (bolder fonts look OK).

What is the best and quickest way to tell my app to use Roboto Light on high-density screens and another font on low-density ones? My first idea was to create different values directories (values-mdpi, values-hdpi, values-xhdpi...), place a strings.xml in each one with the same key and assign it the font's filename I wish to use for each density, and then load the font using that string... however that looks a bit complicated and I was wondering if there is an easier way.

mittelmania
  • 3,393
  • 4
  • 23
  • 48

2 Answers2

1

DisplayMetrics metrics = getResources().getDisplayMetrics();

int densityDpi = (int)(metrics.density)

depends on your density you can set font type to textview and other controls like below code use

Typeface font = Typeface.createFromAsset(getAssets(), "/fonts/Avenir.ttf");

textView.setTypeface(font);

Dakshesh Khatri
  • 639
  • 7
  • 12
  • That's what I ended up doing (I created a new static method that returned the Typeface object created from asset by the display density). Thanks! – mittelmania Sep 04 '14 at 21:08
0

You could do this programmatically by

float density = getResources().getDisplayMetrics().density;

This will give you:

0.75 - ldpi

1.0 - mdpi

1.5 - hdpi

2.0 - xhdpi

3.0 - xxhdpi

4.0 - xxxhdpi

Source

Then check the density and act accordingly:

if (density >= 2.0) {
    textView.setTypeface(Typeface.SANS_SERIF);
} else {
    textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
}
Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116