Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android, how do I get the font type selected on device and also know if it's the default one?

Tags:

android

fonts

Attached below is a screenshot of the fonts on my device. fonts on my device

Here is the requirement:

  • if the user selects the default font, the app should use Roboto
  • if any other font is selected, use the selected font on the app.

Examples:

  1. if selected font = Rosemary, app. font = Rosemary
  2. if selected font = Default, app. font = Roboto

(The default font on the system may or may not be Roboto)

How do I programmatically get the font selected on the system, and also know if it's the default one or not?

I know that if I use Typeface.DEFAULT, it would give me the selected font. But I would also like to know if it's the Default font (as seen in the attached screenshot).

like image 303
Ashok Bijoy Debnath Avatar asked Oct 16 '22 11:10

Ashok Bijoy Debnath


1 Answers

You might want to look into this Typeface. This class provides tools so that you can know which font/typeface is selected for against which type.

For e.g. Look at defaultFromStyle

Typeface.defaultFromStyle(int style)

where the value of Style is an Integer and the values varies from

int: Value is NORMAL, BOLD, ITALIC, or BOLD_ITALIC

Edit 1

After doing a bit of research I found that Typeface doesn't provide the font name. Then i found this SO this gives very useful information and it leads to another SO which has a custom FontListProvider and states that the first font will be the default Font. If you use that custom class then it would be very easy to find the default font.

final List<FontListParser.SystemFont> fonts = FontListParser.safelyGetSystemFonts();
String[] items = new String[fonts.size()];
for (int i = 0; i < fonts.size(); i++) {
    Log.e("Font",fonts.get(i).name);
}
like image 148
Sahil Avatar answered Oct 19 '22 00:10

Sahil