0

I have a list which consist of user selected text with different fonts . User can choose its desired font from this list.

How can I get all font names from font directory programmatically?

Alireza Bideli
  • 834
  • 2
  • 9
  • 31

2 Answers2

3

If you want to get all fonts inside main->res->font directory

Try something like this:

Kotlin

val fontFields = R.font::class.java.fields
val fonts = arrayListOf<Int>()

for (field in fontFields) {
    try {
        Log.i("TAG", field.name)
        fonts.add(field.getInt(null))
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

for(font in fonts){
    val typeface = appContext.resources.getFont(font)
    println(typeface.isBold)
}

Java

Field[] fontFields = R.font.class.getFields();
ArrayList<Integer> fonts = new ArrayList<>();

for (Field field : fontFields) {
    try {
        Log.i("TAG", field.getName());
        fonts.add(field.getInt(null));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

for (int font : fonts){
    Typeface typeFace = appContext.getResources().getFont(font);
    Log.i("TAG", String.valueOf(typeFace.isBold()));
}
murgupluoglu
  • 6,524
  • 4
  • 33
  • 43
0

Take a look here: how-to-retrieve-a-list-of-available-installed-fonts-in-android

another quick way would be to retrieve the android system fonts they are always located in system/fonts

String path = "/system/fonts";
File file = new File(path);
File ff[] = file.listFiles();
NobodyIsPerfect
  • 182
  • 1
  • 12