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