I decided to try material alert dialogs on android. The problem I am having is when I try to apply some styles. Checking the docs, I found this:
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
So I decided to try this:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="materialAlertDialogTheme">@style/Theme.App.MaterialDialogAlert</item>
</style>
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<!-- FIXME: this does not work. it does not change the title appearance. -->
<!-- <item name="materialAlertDialogTitleTextStyle">@style/TextAppearance.App.MaterialDialogAlert.Title.Text</item>-->
<!-- FIXME: this change only the title font, not the message appearance -->
<item name="android:fontFamily">@font/nunito_semi_bold</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button.TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="TextAppearance.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
But so far I could style the buttons successfully. With the title and message I have some issues. Now I am able to apply some styles to the title with this:
<item name="android:fontFamily">@font/nunito_semi_bold</item>
But I do not think this is the proper way. First this does not apply the font to the message section. Second, I would like to apply other text styles, one for title and one for the message section.
I tried also checking this style: MaterialAlertDialog.MaterialComponents.Title.Text
where I can see that this item is being applied at some point:
<item name="android:textAppearance">?attr/textAppearanceSubtitle1</item>
So I decided to declare this in my base theme:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
but nothing changed.
So my questions are:
EDIT I tried using this too:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="materialAlertDialogTitleTextStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
<item name="materialAlertDialogBodyTextStyle">@style/TextAppearance.App.MaterialDialogAlert.Body.Text</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button.TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">@font/nunito_semi_bold</item>
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
But it is not working.
Also I tried this from the material components repo:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="materialAlertDialogTitleTextStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
<item name="materialAlertDialogBodyTextStyle">@style/Dialog.App.MaterialDialogAlert.Body.Text</item>
<item name="buttonBarButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Body.Text" parent="@style/MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textAppearance">@style/TextAppearance.Body2</item>
<item name="fontFamily">@font/nunito_regular</item>
<item name="android:fontFamily">@font/nunito_regular</item>
<item name="android:textColor">@color/color_error</item>
</style>
but does not work.
The only think I could customise was the title by adding this to the theme:
<item name="android:windowTitleStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
I was checking the code inside the library and I can see that this layout is being used: mtrl_alert_dialog.xml
There I can see this:
<TextView
android:id="@android:id/message"
style="?attr/materialAlertDialogBodyTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"/>
where we can see a reference to materialAlertDialogBodyTextStyle. So maybe it is a bug in the library?
You can use this style to customize the title and the body:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Title -->
<item name="materialAlertDialogTitleTextStyle">@style/MyTitle_MaterialAlertDialog</item>
<!-- Body -->
<item name="materialAlertDialogBodyTextStyle">@style/BodyTextAppearance.MaterialComponents.Body2</item>
</style>
Then define the style for the title:
<style name="MyTitle_MaterialAlertDialog" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/MyTitle_TextAppearance.MaterialComponents.Subtitle1</item>
</style>
<style name="MyTitle_TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">.....</item>
<item name="android:fontFamily">....</item>
<item name="android:textStyle">.....</item>
</style>
Then define for the body:
<style name="BodyTextAppearance.MaterialComponents.Body2" parent="@style/MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">@color/....</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
<item name="fontFamily">.....</item>
<item name="android:fontFamily">....</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With