Center message in android dialog box
Create your own TextView object and then supply it to popup builder as View:
AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
popupBuilder.setView(myMsg);
You can control all other text parameters (style, color, size ...). To control margins you may programatically create LinearLayout, set LayoutParams, and then put TextView into it.
How to center text in Alert Dialog Builder?
Instead of setting the message, use AlertDialog.Builder.setView(View)
with a View
with centred text
Kotlin - Center message in AlertDialog
Change R.id.message
to android.R.id.message
since you want a view that was declared in the android package.
val mw = alertDialog.findViewById<TextView>(android.R.id.message)
mw.gravity = Gravity.CENTER
Centre message in AlertDialog box
Add android:gravity="center"
to TextView
with id R.id.title_text
in your layout R.layout.simple_dialog
.
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
To center default message TextView
you have to get the TextView
from dialog after show()
.
alertDialog.show();
TextView messageView = (TextView)alertDialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
I do not know if its a good approach but you can also set the custom view to whole dialog then things can be handled easily.
how to make center MaterialAlertDialogBuilder alignment title, message & button
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()
Custom dialog on Android: How can I center its title?
Just found this post while trying to figure out how to do the same thing. Here's how I did it for anyone else that finds this in the future.
Style xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowTitleStyle">@style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
And in my activities onCreateDialog method for the dialog I want styled I create the dialog like this:
Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
pauseDialog.setTitle(R.string.pause_menu_label);
pauseDialog.setContentView(R.layout.pause_menu);
align AlertDialog buttons to center
This worked for me :
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = builder.create();
dialog.show(); //show() should be called before dialog.getButton().
final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
positiveButtonLL.gravity = Gravity.CENTER;
positiveButton.setLayoutParams(positiveButtonLL);
Related Topics
:App:Dexdebug Execexception Finished with Non-Zero Exit Value 2
Is Dalvik's Memory Model the Same as Java'S
How to Close Another App in Android
How to Create JSON Object Using String
Issue: Passing Large Data to Second Activity
Intellij Build Error Context Mismatch
Trivial: Get Confirmation of Email Sent in Android
Why Ruby Modulo Is Different from Java/Other Lang
How to Install Various Python Libraries in Jython
What Is the Easiest Way to Parse an Ini File in Java
Java.Lang.Noclassdeffounderror: Com.Google.Ads.Adview
How to Change the Edittext Text Without Triggering the Text Watcher
Why Menuitemcompat.Getactionprovider Returns Null
How to Print to the Console in Android Studio
Android Color Notification Icon
Multiple Dex Files Define <My Package>/Buildconfig, Can't Find the Cause: