How to use Font Awesome icon in android application?

后端 未结 9 2108
时光说笑
时光说笑 2020-12-02 20:58

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

9条回答
  •  情话喂你
    2020-12-02 21:52

    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

    1. add FA fonts lib: implementation 'com.mikepenz:fontawesome-typeface:5.9.0.0-kotlin@aar'

    2. add an ext lib iconics:
      implementation "com.mikepenz:iconics-core:4.0.2"

    3. 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) {
           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.

提交回复
热议问题