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
In your layout:
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextAppearanceActive="@style/BottomNavigationViewTextStyle"
app:itemTextAppearanceInactive="@style/BottomNavigationViewTextStyle"
... />
In your styles.xml:
<style name="BottomNavigationViewTextStyle">
...
<item name="android:fontFamily">@font/whatever_font</item>
...
</style>
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:
<com.example.projectname.FontBottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
@Arash answer helped:
If you have a CustomFont in "Asset Folder" and you want to set in your "Bottom Navigation" use this code
public static void persian_iran_font(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
persian_iran_font(context, child);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "teshrinarmedium.otf"));
}
} catch (Exception e) {
}
}
And then use methode in your MainActivity Like This
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
persian_iran_font(getApplicationContext(), navigation);
kotlin Version
fun persian_iran_font(context: Context, v: View) {
try {
if (v is ViewGroup) {
val vg = v as ViewGroup
for (i in 0 until vg.childCount) {
val child: View = vg.getChildAt(i)
persian_iran_font(context, child)
}
} else if (v is TextView) {
(v as TextView).setTypeface(
Typeface.createFromAsset(
context.getAssets(),
"teshrinarmedium.otf"
)
)
}
} catch (e: Exception) {
}
}
Goodluck
Kotlin language:
1: Create a file with a custom name, ex: BottomNavigationViewExtension.kt
2: Put the code below:
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.android.material.bottomnavigation.BottomNavigationView
fun BottomNavigationView.changeNavTypeface(typeface: Typeface) {
val view: View = this
checker(view, typeface)
}
private fun checker(view: View, typeface: Typeface) {
if (view is ViewGroup) {
for (i in 0 until view.childCount) {
val child = view.getChildAt(i)
checker(child, typeface)
}
} else if (view is TextView) {
view.typeface = typeface
}
}
3: Usage:
navView.changeNavTypeface(
Typeface.createFromAsset(
assets,
"fonts/IRANSansMobile.ttf"
)
)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Widget.BottomNavigationView"
parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">@font/segoe_ui_semibold</item>
</style>
</resources>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFF"
app:itemIconTint="@color/navigation_bottom"
app:itemTextColor="@color/navigation_bottom"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:theme="@style/Widget.BottomNavigationView" (this line)
app:menu="@menu/nav_items" />