4

I've recently finished building a project, and my next goal was to create a build flavor specially for RTL, one important note was that I need to force the RTL layout, independent from the device language.

So I managed to force RTL on most of the app using the view method setLayoutDirection and rotated all the views that didn't cooperate , but for some reason, I couldn't mirror the dialogs in the app, which appear to flip only when the device language change. I tried to flip the dialog in the onCreateDialog, accessing getView, which returned null, and I'm not sure what else I can do ...

Could someone help me flip the DialogFragment to RTL?

Thanks.

Krouitch
  • 531
  • 3
  • 13
Nadav96
  • 1,051
  • 1
  • 14
  • 25

1 Answers1

4

You can use getWindow().getDecorView().setLayoutDirection()for AlartDialog, The layout Direction will change for title and content but not for the buttons.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);

    AlertDialog alertDialog = builder.create();

    // Here you can change the layout direction via setLayoutDirection()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        alertDialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }

    return alertDialog;
}
Motaz Alnuweiri
  • 106
  • 1
  • 6