I want to use Font Awesome\'s icon set in my android application. I have some TextView
to set those icons. I don\'t want to use any png image. My Textview is li
previous cases are OK, but just imagine you need a dynamically generated menu items based on data obtained from api. We don't now what exact icon should be in each item before we obtain pages model. Of course we can put all the FA icons to String resource as shown before, but also you can insert FA icon by name just in fly
let's do it
add FA fonts lib: implementation 'com.mikepenz:fontawesome-typeface:5.9.0.0-kotlin@aar'
add an ext lib iconics:
implementation "com.mikepenz:iconics-core:4.0.2"
do come code (kotlin):
//add class property in your activity
private lateinit var navView: NavigationView
//find your navigation view on activity onCreate
navView = findViewById(R.id.event_details_nav_view)
//call your updateNavigation(pages) function when you received data with menu items. i.e. Page is a data object with Id, Name, faIcon (it's name)
//add function where we create menu items based on pages model
fun updateNavigation(pages: List<Page>) {
pages.forEach {
navView.menu.add(0, it.id, 0, it.name)
.apply {
IconsHelper.getFontAwesomeIcon(
this,
name,
24,
R.color.nav_drawer_item_icon_color_default
).let {
menuItem.icon = it
}
}
}
}
and all the magic is in IconHelper, let's do it
// add IconHelper.kt and paste it there
import android.content.Context
import com.mikepenz.iconics.Iconics.findFont
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp
object IconsHelper {
private const val defaultIconName = "faw_question_circle"
fun getFontAwesomeIcon(context: Context, name: String, size: Int, color: Int): IconicsDrawable {
return when(name.isEmpty()) {
true ->
getIcon(defaultIconName, size, color, context)
false -> {
normalizeIconName(name)
.let {
when(iconExists(context, it)) {
true ->
getIcon(it, size, color, context)
false ->
getIcon(IconsHelper.defaultIconName, size, color, context)
}
}
}
}
}
private fun getIcon(name: String, size: Int, color: Int, context: Context): IconicsDrawable {
return IconicsDrawable(context, name)
.apply {
sizeDp(size)
colorInt(color)
}
}
private fun normalizeIconName(name: String): String {
name
.replaceFirst("fa", "faw")
.replace("-", "_")
.let {
return when(it.contains("sliders")) {
true ->
"faw_sliders_h"
false ->
it
}
}
}
private fun iconExists(context: Context, icon: String): Boolean {
try {
findFont(icon.substring(0, 3), context)
.let {
it!!.getIcon(icon)
}
return true
} catch (e: Exception) {
print(e)
}
return false
}
}
p.s. look at normalizeIconName() function, maybe you don't need it.
You can Use FontAwesome, just declare in String.xml
<resources>
<string name="fa_icon_areachart"></string>
<string name="fa_icon_piechart"></string>
<string name="fa_icon_linechart"></string>
</resources>
If anyone wants to assign font value programmatically then this is how I did. Create an assets folder if it's not there and put ttf file in the folder
Typeface type = Typeface.createFromAsset(getContext().getAssets(), "fonts/fa_solid_900.ttf");
tvHeart.setTypeface(type);
tvHeart.setText(Html.fromHtml(""));