2

how to make center alignment title, message & button

MaterialAlertDialogBuilder(requireContext())
                .setTitle("Message")
                .setMessage("This is info message.")
                .setPositiveButton("Ok") { dialog, which ->
             
                }
                
                .show()
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
  • 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

5 Answers5

10

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
  • 320,139
  • 94
  • 887
  • 841
  • it is working thank u, do you have any idea about center alignment button as well as custom font (title, subtitle & button) – Bolt UIX 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
2

In the simplest way

MaterialAlertDialogBuilder(context, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
Mohamed Rahal
  • 61
  • 1
  • 3
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). – Bolt UIX 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
  • 8,172
  • 5
  • 41
  • 44
0

Change to this

MaterialAlertDialogBuilder(requireContext(), com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered))
                    .setTitle("Message")
                    .setMessage("This is info message.")
                    .setPositiveButton("Ok") { dialog, which ->
                 
                    }
                    
                    .show()
Angel
  • 190
  • 2
  • 10