I have created a bottom bar navigation in my android page. But now I want to apply the custom font-family in bottom navigation texts.
This is the bottom navigation c
Kotlin Code of custom BottomNavigationView to set custom font:
1.Keep a font to assets directory of your android studio project. Here I used my font "SolaimanLipi_20-04-07.ttf"
2.Copy below kotlin code and paste to your android studio project.
class FontBottomNavigationView : BottomNavigationView {
constructor(context: Context) : super(context) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
}
private var fontFace: Typeface? = null
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
val bottomMenu = getChildAt(0) as ViewGroup
val bottomMenuChildCount: Int = bottomMenu.childCount
var item: BottomNavigationItemView? = null
var itemTitle: View? = null
var shiftingMode: Field? = null
if (fontFace == null){
fontFace = Typeface.createFromAsset(context.assets, "SolaimanLipi_20-04-07.ttf") // font from assets directory
}
try {
shiftingMode = bottomMenu.javaClass.getDeclaredField("mShiftingMode")
shiftingMode.isAccessible = true
shiftingMode.setBoolean(bottomMenu, false)
shiftingMode.isAccessible = false
} catch (e: Exception){
e.printStackTrace()
}
for (i in 0..bottomMenuChildCount){
try {
item = bottomMenu.getChildAt(i) as BottomNavigationItemView
itemTitle = item.getChildAt(1)
((itemTitle as BaselineLayout).getChildAt(0) as AppCompatTextView).typeface = fontFace
((itemTitle as BaselineLayout).getChildAt(1) as AppCompatTextView).typeface = fontFace
} catch (e: Exception){
e.printStackTrace()
}
}
}}
3.Use on xml file as like below:
@Arash answer helped: