Noor Ali Butt
Noor Ali Butt

Reputation: 817

Right to left layout support

I'm trying to support Arabic language right to left layout support but turns out there is no way you can support right to left in Xamarin Forms.

I even tried dependency service and called right to left layout by myself. Here is the code:

public CultureInfo SetLocale(string locale)
    {
        var netLocale = locale.Replace("_", "-");
        var ci = new CultureInfo(netLocale);

        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;

        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr1)
        {
            // Change locale settings in the app.
            var dm = Forms.Context.Resources.DisplayMetrics;
            var conf = Forms.Context.Resources.Configuration;
            conf.Locale = new Java.Util.Locale(locale);
            Forms.Context.Resources.UpdateConfiguration(conf, dm);

            if (locale == "en")
                (Forms.Context as MainActivity).Window.DecorView.LayoutDirection = Android.Views.View.LayoutDirectionLtr;
            else
                (Forms.Context as MainActivity).Window.DecorView.LayoutDirection = Android.Views.View.LayoutDirectionRtl;
        }

        return ci;
    }

Upvotes: 1

Views: 1514

Answers (2)

Roshna Omer
Roshna Omer

Reputation: 721

I hope I'm not too late, here are some links:

there is layoutDirection and on this forum they have answered it:

To detect when RTL is required, I use this bit of code (I found reflection was required, as the IsRightToLeft property was unexpectedly inaccessible):

bool rightToLeft = false;
PropertyInfo propertyInfo = thisCulture.TextInfo.GetType().GetRuntimeProperty("IsRightToLeft");
object value = propertyInfo?.GetValue(thisCulture.TextInfo);
if (value is bool)
    rightToLeft = (bool) value;

After that, it was mostly a case of switching a few properties:

 HorizontalOptions = rightToLeft ? LayoutOptions.EndAndExpand : LayoutOptions.StartAndExpand,
 HorizontalTextAlignment = rightToLeft ? TextAlignment.End : TextAlignment.Start;

more links Adjust layout and fonts, and support RTL

Upvotes: 1

lordkain
lordkain

Reputation: 3109

You could set the dir property of html if language is arabic.

<html dir="rtl">

Upvotes: -7

Related Questions