Right to left(RTL)

3 Sep 20204 minutes to read

TreeView supports the right-to-left by setting LayoutDirection to Rtl.TreeView also supports right-to-left when device layout direction is changed.

NOTE

Specific setup is required to enable right-to-left. For android settings you can refer here.

SfTreeView treeView = new SfTreeView();
treeView.LayoutDirection = LayoutDirection.Rtl;

NOTE

If you need to customize Adapter with your custom views, layout the views in based on LayoutDirection to respond for right-to-left changes.

//customized view to support rtl
public class NodeImageView : LinearLayout
{
    private ContentLabel label1;
    private ImageViewExt imageIcon;
    SfTreeView view;

    public NodeImageView(Context context, SfTreeView treeView) : base(context)
    {
        view = treeView;
        this.Orientation = Orientation.Horizontal;
        label1 = new ContentLabel(context);
        label1.Gravity = GravityFlags.CenterVertical;
        if(view.LayoutDirection == Android.Views.LayoutDirection.Rtl)
            label1.TextDirection = TextDirection.Rtl;
        imageIcon = new ImageViewExt(context);
        this.AddView(imageIcon);
        this.AddView(label1);
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        var density = Resources.DisplayMetrics.Density;
        var measuredWidth = (int)(40 * density);
        var measuredHeight = (int)(45 * density);
        var labelWidth = Math.Abs(widthMeasureSpec - measuredWidth);
        this.label1.SetMinimumHeight(measuredHeight);
        this.label1.SetMinimumWidth(labelWidth);
        this.imageIcon.SetMinimumHeight(measuredHeight);
        this.imageIcon.SetMinimumWidth(measuredWidth);
        this.imageIcon.Measure(measuredWidth, measuredHeight);
        this.label1.Measure(labelWidth, measuredHeight);
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        var density = Resources.DisplayMetrics.Density;
        var measuredWidth = (int)(40 * density);
        var measuredHeight = (int)(45 * density);
        if (view.LayoutDirection == Android.Views.LayoutDirection.Rtl)
        {
            this.imageIcon.Layout(Width- measuredWidth, 0, Width, measuredHeight);
            this.label1.Layout(0, 0, Width- measuredWidth, measuredHeight);
        }
        else
        {
            this.imageIcon.Layout(0, 0, measuredWidth, measuredHeight);
            this.label1.Layout(measuredWidth, 0, Width, measuredHeight);
        }
    }
}

You can download the entire source code here.

Xamarin Android TreeView with right-to-left localization