Bolt UIX
Bolt UIX

Reputation: 7022

how to make center MaterialAlertDialogBuilder alignment title, message & button

how to make center alignment title, message & button

MaterialAlertDialogBuilder(requireContext())
                .setTitle("Message")
                .setMessage("This is info message.")
                .setPositiveButton("Ok") { dialog, which ->
             
                }
                
                .show()

Upvotes: 3

Views: 2642

Answers (5)

Angel
Angel

Reputation: 190

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()

Upvotes: 0

Mohamed Rahal
Mohamed Rahal

Reputation: 73

In the simplest way

MaterialAlertDialogBuilder(context, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)

Upvotes: 2

Juan Cruz Soler
Juan Cruz Soler

Reputation: 8254

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

<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>

Upvotes: 0

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363905

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

Upvotes: 13

danms07
danms07

Reputation: 141

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()

Upvotes: 0

Related Questions