Issue
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?
Solution
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()));
}
Answered By - murgupluoglu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.