0

how to make center alignment title, message & button

MaterialAlertDialogBuilder(requireContext())
                .setTitle("Message")
                .setMessage("This is info message.")
                .setPositiveButton("Ok") { dialog, which ->
             
                }
                
                .show()
Gabriele Mariotti
  • 250,295
  • 77
  • 670
  • 690
Hari Shankar S
  • 3,330
  • 4
  • 19
  • 36
  • Does this answer your question? [Center message in android dialog box](https://stackoverflow.com/questions/4954130/center-message-in-android-dialog-box) – m'hd semps Jun 24 '20 at 20:12

3 Answers3

7

For the the title and the message you can use:

    <style name="MaterialAlertDialog__Center" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="materialAlertDialogTitlePanelStyle">@style/Title.Panel.Center</item>
        <item name="materialAlertDialogBodyTextStyle">@style/Message.Center</item>
    </style>

    <style name="Title.Panel.Center" parent="MaterialAlertDialog.MaterialComponents.Title.Panel">
        <item name="android:gravity">center_horizontal</item>
    </style>

    <style name="Message.Center" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
        <item name="android:gravity">center_horizontal</item>
    </style>

with:

MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_Center)
                .setTitle("Message")
                ..
                .show()

enter image description here

Gabriele Mariotti
  • 250,295
  • 77
  • 670
  • 690
  • it is working thank u, do you have any idea about center alignment button as well as custom font (title, subtitle & button) – Hari Shankar S Jun 25 '20 at 04:48
  • 1
    @HariShankarS About the alignment I don't know a style solution. There aren't public method to do it. About the text, font, color of the button you can check this answer: https://stackoverflow.com/a/58043299/2016562 – Gabriele Mariotti Jun 25 '20 at 06:18
0

You can create your own layout and add it to your dialog

val inflater=LayoutInflater.from(requireContext())
//Getting the custom view
val view=inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT,null)
//Obtaining the components
val title=view.findViewById(R.id.YOUR_CUSTOM_TILE)
title.text="Message"
val button=view.findViewById(R.id.YOUR_CUSTOM_BUTTON)
button.setOnCLickListener{
    //onClick
    }
val dialog=MaterialAlertDialogBuilder(requireContext()).setView(view).create()
dialog.show()
danms07
  • 141
  • 4
  • thank you for your suggestion, I am looking style based solution (center alignment with custom font: material dialog title, subtitle & button). – Hari Shankar S Jun 25 '20 at 04:50
0

To center the title and message you only have to change this in your Theme

<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>
Juan Cruz Soler
  • 7,914
  • 5
  • 38
  • 44