1

Been fighting for hours about changing the alignment of the buttons inside AlertDialog (support.v7 one), since they won't align themselves according to the locale view direction, despite the whole app DOES align to left and also the text inside the AlertDialog.

(Why would this happen you say? I'm programatically configuring the locale language to be "en" since that's my default app language, even though the system locale might be something else).

So like I said, I don't need to touch the message inside the dialog, but as an example, that's how to change it's direction:

TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.RIGHT); // or LEFT

Of course it doesn't work on the buttons, since I need to change the layout gravity instead.

Here's how I find the buttons (after I call show() on the AlertDialog.Builder of course, else they would be null):

AppCompatButton accept = (AppCompatButton)dialog.findViewById(android.R.id.button1);
AppCompatButton cancel = (AppCompatButton)dialog.findViewById(android.R.id.button2);

And here's how I attempt to change their alignment inside their parent LinearLayout:

((LinearLayout.LayoutParams)accept.getLayoutParams).gravity = Gravity.RIGHT;
((LinearLayout.LayoutParams)cancel.getLayoutParams).gravity = Gravity.RIGHT;

I chose RIGHT since the side of the buttons inside the dialog is always opposite to the side which the text is aligned to. (Yes - I tried LEFT also, nothing changed).

This doesn't work. Does anyone have an idea how to achieve this? It seems they are just stuck to their place.

Edit: Title isn't aligned also, I just confirmed this (for some reason it appears on the right, like my system configuration, and not like my locale configuration).

Jjang
  • 11,250
  • 11
  • 51
  • 87

2 Answers2

3

The problem isn't the Gravity settings... When you look at the xml source (../sdk/platforms/android-23/data/res/layout/alert_dialog_material.xml), the layout's AlertDialog contains this:

<LinearLayout android:id="@+id/buttonPanel"
    style="?attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" ...>

    <Button android:id="@+id/button3"
        style="?attr/buttonBarNeutralButtonStyle" ... />

    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="invisible" />

    <Button android:id="@+id/button2"
        style="?attr/buttonBarNegativeButtonStyle" ... />

    <Button android:id="@+id/button1"
        style="?attr/buttonBarPositiveButtonStyle" ... />
</LinearLayout>

There is a Space view with the buttons. This view fills the container with its weight. Therefore, you actually have a widget which pushes the buttons at the right's parent container.

A simple solution might to get the parent's buttons container and remove the Space element before setting a gravity.

// get the container
LinearLayout containerButtons = (LinearLayout) dialog.findViewById(R.id.buttonPanel); 
// remove the Space view
containerButtons.removeView(containerButtons.getChildAt(1)); 
// set a gravity to the children buttons
containerButtons.setGravity(Gravity.RIGHT); 

However, you should create and use your own custom layout, in case of future Google's development changes might occurring.

Blo
  • 11,903
  • 5
  • 45
  • 99
  • 1
    This is not a perfect solution as Google might update this layout and change the `Space` child view. and this will gives a certain `NullPointerException`. Currently, this view does not have an `Id` so I can't be sure of future changes on this layout. – blueware Feb 21 '17 at 10:57
1

You can completely customize an AlertDialog. The trick here is probably to use a custom view for the dialog, and create your own buttons in that view.

For an example, see How can can I add custom buttons into an AlertDialog's layout?

Community
  • 1
  • 1
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • But I want the view to look native, not custom. I don't see why I need to create a whole custm one just to align the buttons... But if you think it is the only way, then, I'd take the original layout of the AlertDialog and change it a little. Do you know where to find it? – Jjang Nov 10 '15 at 09:10
  • An additional thing to try...if you are currently setting the gravity of the buttons' layoutParams before showing the dialog, try doing it after dialog.show() instead. – GreyBeardedGeek Nov 10 '15 at 16:03
  • I'm doing it after, of course :( Wow, those buttons just won't move huh? – Jjang Nov 10 '15 at 19:36