Home > Mobile >  Set custom font for message text of AlertDialog
Set custom font for message text of AlertDialog

Time:09-20

I have a simple alert dialog to being shown in my app and I want to set custom font for title and message texts, even though I set a style by adding fontFamily attr. into it it doesnt effect on message text(title got the correct font somehow)

what I've tried so far is below:

ContextThemeWrapper

Context context = new ContextThemeWrapper(context, R.style.NetworkAlertDialogStyle);`

AlertDialog.Builder builder = new AlertDialog.Builder(context);

Style

<style name="NetworkAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/baloo2_bold</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/black</item>
</style>

Passing style to constructor of AlertDialog builder

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.NetworkAlertDialogStyle);


<style name="NetworkAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:fontFamily">@font/baloo2_regular</item>
</style>

CodePudding user response:

Try This

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="colorSurface">@color/white</item>
        <item name="android:layout_gravity">center</item>
        <item name="materialAlertDialogTitleTextStyle">@style/TitleTextStyle</item>
        <item name="materialAlertDialogBodyTextStyle">@style/BodyTextStyle</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle1</item>
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle1</item>
        <item name="android:colorControlHighlight">@color/tool_icon_press</item>
    </style>
    <style name="TitleTextStyle" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
        <item name="android:textColor">@color/text_color</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
        <item name="android:textSize">@dimen/_14sdp</item>
    </style>
    <style name="BodyTextStyle" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
        <item name="android:textColor">@color/text_color</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
    </style>
    <style name="PositiveButtonStyle1" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/text_color</item>
        <item name="rippleColor">@color/tool_icon_press</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>

    </style>
    <style name="NegativeButtonStyle1" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/text_color</item>
        <item name="rippleColor">@color/tool_icon_press</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
    </style>

use like this

new Dialog(context, R.style.AlertDialogTheme)

CodePudding user response:

Make custom dialog dialog and than you can set font family accordingly

  • Related