Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Created August 4, 2022 06:29
Show Gist options
  • Select an option

  • Save shibbirweb/d0925bf63ff8084d4898e9557ce812fa to your computer and use it in GitHub Desktop.

Select an option

Save shibbirweb/d0925bf63ff8084d4898e9557ce812fa to your computer and use it in GitHub Desktop.
Android: Dynamically set font for ASCII and UNICODE text
@BindingAdapter(value = ["android:fontFamilyByText"])
fun TextView.customFontFamilyByText(text: String?) {
val isAscii = Charsets.US_ASCII.newEncoder().canEncode(text)
val fontFamily = if (isAscii) {
ResourcesCompat.getFont(context, R.font.inter_variable_font_slnt_wght);
} else {
ResourcesCompat.getFont(context, R.font.noto_sans_bengali_variable_font_wdth_wght);
}
setTypeface(fontFamily, typeface.style)
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Any content"
android:textSize="60sp"
android:fontFamilyByText="@{`Same content as text for checking text is unicode or ascii`}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment