Mehdi Haghgoo
09/08/2020, 9:44 AMclass MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DraftNotebookTheme {
Surface(color = MaterialTheme.colors.background) {
MyButton("Android")
}
}
}
}
}
@Composable
fun MyButton(label: String) {
Button(onClick = {}) {
Text(label)
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
DraftNotebookTheme {
MyButton("سلام دنیا")
}
}
When doing preview, the text shown on the button does not follow the font family I have set in the theme. Is this a bug?efemoney
09/08/2020, 9:46 AMDraftNotebookTheme
Mehdi Haghgoo
09/08/2020, 9:47 AM@Composable
fun DraftNotebookTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
MaterialTheme(
colors = colors,
typography = typography,
shapes = shapes,
content = content
)
}
val typography = Typography(
body1 = TextStyle(
fontFamily = fontFamily(font(R.font.iransans)),
fontWeight = FontWeight.Normal,
fontSize = 12.sp,
textDirection = TextDirection.Rtl
)
)
efemoney
09/08/2020, 10:25 AMButton
to confirm but thats correct behavior. You are setting fontFamily for only body1
, meanwhile Button
composable provides MaterialTheme.typography.button
as textStyle. If you look at the constructor for Typography
it has default values for the rest that uses the default font family passed. The fix is to set the defaultFontFamily
while constructing your Typography
efemoney
09/08/2020, 10:29 AMMehdi Haghgoo
09/08/2020, 11:04 AMbutton
parameter of typography, the button is following the theme font.
val myFont = fontFamily(font(R.font.iransans))
val typography = Typography(
body1 = TextStyle(
fontFamily = fontFamily(myFont),
fontWeight = FontWeight.Normal,
fontSize = 12.sp,
textDirection = TextDirection.Rtl
),
override
button = TextStyle(
fontFamily = fontFamily(myFont),
fontWeight = FontWeight.W500,
fontSize = 14.sp
),
)
efemoney
09/08/2020, 11:05 AMdefaultFontFamily
if not you’ll still use the system default for components that dont use body1
or button
styles