Check out the Typeface object family in Android

Is it possible to check in which family the Typeface object is used in Android API 8?

I create a Typeface for the Paint object this way

//Simplified code, the user actually selects the family and style from a list Paint paint = new Paint(); paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)); 

Later I would like to check out the family like this

 Typeface tf= paint.getTypeface(); if (tf.equals(Typeface.DEFAULT) || tf.equals(Typeface.DEFAULT_BOLD)) { //do something } else if (...) { //do something else } 

This does not work, since they are not the same object, I also tried tf == value , but again the result. The Typeface object does not show anything useful to help with family discovery, is there a workaround for this? I only need to identify the main Android fonts (SERIF, SANS-SERIF, DEFAULT, MONOSPACE, etc.)

+4
source share
2 answers

Not easy. The key to the family is native_instance int , which is a closed package and does not appear directly, as you can see in the source code .

There are various unpleasant ways to access this information that may or may not work on all devices and in all past / present / future versions of Android.

Perhaps you could create your own wrapper around a Typeface that tracks this information.

+3
source

If you look at the sources, you will see that by default they are created with the DEFAULT (*) style, therefore:

 Typeface paintTf = paint.getTypeface(); Typeface.SERIF.equals(Typeface.create(paintTf, Typeface.DEFAULT)); 

can do the job (here I check if the family is a serif).

NB: (*) Not valid for DEFAULT_BOLD.

+1
source

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


All Articles