Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setLayoutDirection on DialogFragment

Tags:

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.

like image 915
Nadav96 Avatar asked Jun 04 '16 00:06

Nadav96


People also ask

How do I disable RTL on android?

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”.

What is the difference between dialog and DialogFragment?

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.

How do I change RTL on android?

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.

What is DialogFragment used for?

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.


1 Answers

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;
}
like image 187
Motaz Alnuweiri Avatar answered Sep 28 '22 03:09

Motaz Alnuweiri