Ghasan غسان
Ghasan غسان

Reputation: 5877

Can't force RTL on Android

When using FolowDirection with RightToLeft for elements in XAML, it works fine.

However, I want to set it by default and force it throughout the app.

Followed the docs but nothing is happening, setting Window.DecorView.LayoutDirection does not seem to do anything.

I have also set: <application android:label="App.Android" android:supportsRtl="true"></application>

Here is my Android project code:

protected override void OnCreate(Bundle savedInstanceState)
{
    Window.DecorView.LayoutDirection = Android.Views.LayoutDirection.Rtl;

    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Upvotes: 0

Views: 468

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

I want to set it by default and force it throughout the app.

Firstly , we should know that the direction of the app is based on the device language and region .So you need to make sure that the setting of language and region is RTL (like Arabic) .

You could check it in code behind

var flowDirection = Device.FlowDirection; // this is a readonly property 

If you do want to set your app as RTL while don't change the system setting . You could create a base ContentPage and setting the FlowDirection of it . The value will be inherited by all of its children (unless you override it) .

public partial class BaseContentPage : ContentPage
{
  public BaseContentPage()
  {
    InitializeComponent();
    FlowDirection = FlowDirection.RightToLeft;
  }
}

If you want to let a specific element (like a label) still LTR , you just need to set the FlowDirection property on the label to LeftToRight.

Upvotes: 1

Related Questions