'Right justify text in AlertDialog

Is it possible to right-justify the text in an AlertDialog's title and message?

I am showing Hebrew messages but they are showing up left justified.



Solution 1:[1]

This is an old question, but there is a very simple solution. Assuming you are using MinSdk 17, you can add this to your styles.xml:

<style name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="android:layoutDirection">rtl</item>
</style>

And in the AlertDialog.Builder you just need to specify this AlertDialogCustom in the constructor:

new AlertDialog.Builder(this, R.style.AlertDialogCustom)
                    .setTitle("Your title?")
                    .show();

Solution 2:[2]

If you want a quick and easy way to do this, I would just create and modify the default alert using AlertDialog.Builder. You can even create a convenience method and class like so:

/** Class to simplify display of alerts. */
public class MyAlert {

    public static void alert(Context context, String title, String message, OnClickListener listener)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("My Title");
        builder.setMessage("My message");
        builder.setPositiveButton("OK", listener);
        AlertDialog dialog = builder.show();

        // Must call show() prior to fetching views
        TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
        messageView.setGravity(Gravity.RIGHT);

        TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
        if (titleView != null) {
            titleView.setGravity(Gravity.RIGHT);
       }
    }
}

Of course you could also change the gravity to CENTER for center alignment instead of right or the default left.

Solution 3:[3]

After struggling with this for an hour (the solutions above didn't work for me), I managed to align the AlertDialog's title and message to the right, without overriding any styles, simply changing the gravity and layout params.

In DialogFragment:

@Override
public void onStart() {
    super.onStart();

    // Set title and message
    try {
        alignDialogRTL(getDialog(), this);
    }
    catch (Exception exc) {
        // Do nothing
    }
}

The actual function:

public static void alignDialogRTL(Dialog dialog, Context context) {
    // Get message text view
    TextView message = (TextView)dialog.findViewById(android.R.id.message);

    // Defy gravity
    message.setGravity(Gravity.RIGHT);

    // Get title text view
    TextView title = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));

    // Defy gravity (again)
    title.setGravity(Gravity.RIGHT);

    // Get title's parent layout
    LinearLayout parent = ((LinearLayout)Title.getParent());

    // Get layout params
    LinearLayout.LayoutParams originalParams = (LinearLayout.LayoutParams)parent.getLayoutParams();

    // Set width to WRAP_CONTENT
    originalParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;

    // Defy gravity (last time)
    originalParams.gravity = Gravity.RIGHT |  Gravity.CENTER_VERTICAL;

    // Set updated layout params
    parent.setLayoutParams(originalParams);
}

Solution 4:[4]

The kotlinish way :

val alertDialog = alertDialogBuilder.create()

alertDialog.setOnShowListener {

   alertDialog.window?.decorView?.layoutDirection = View.LAYOUT_DIRECTION_RTL
                               }

alertDialog.show()

Solution 5:[5]

For setting layout direction of alert dialog to RTL you can use OnShowListener method. after setting title , message , .... use this method.

dialog = alertdialogbuilder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dlg) {
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextSize(20); // set text size of positive button
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(Color.RED); set text color of positive button

                dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
            }
        });
        dialog.show();

Solution 6:[6]

All you need is to create a style tag in res/values/styles and write :

<style name="alertDialog LayoutDirection">
<item name="android:layoutDirection">rtl</item>

and then add this after alert dialog initializing in your activity :

alertDialog.setView(R.style.alertDialog);

for example :

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        alertDialog.setMessage("??? ????")
                .setCancelable(true);
        alertDialog.setPositiveButton("???", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setNegativeButton("??", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "nooo", Toast.LENGTH_SHORT).show();
            }
        });

        alertDialog.show();
        alertDialog.setView(R.style.alertDialog);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MMG
Solution 2 n8tr
Solution 3
Solution 4 jafar_aml
Solution 5
Solution 6 Hamed samsami