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.
How do I disable RTL support on android? Change all of your app's "left/right" layout properties to new "start/end" equivalents. If you are targeting your app to Android 4.2 (the app's targetSdkVersion or minSdkVersion is 17 or higher), then you should use “start” and “end” instead of “left” and “right”.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.
DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With