How to get all font directory font names in andorid?

Alireza Bideli :

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?

murgupluoglu :

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()));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164196&siteId=1