Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Tool Bar Center using Effects in Xamarin forms

I am trying to center the title of the page in Android in Xamarin forms. I have access to the tool bar and was able to set the font family. But I am not able to set the text alignment or gravity to center.

var context = (Android.App.Activity)Forms.Context; var toolbar = context.Resources.GetIdentifier("toolbar", "id", context.PackageName); var view = context.FindViewById(toolbar);

I tried the following:

ForegroundGravity

view.SetForegroundGravity(GravityFlags.Center);

Text Alignment

view.TextAlignment = Android.Views.TextAlignment.Center;

Access the Childs of the toolbar and set the direction and alignment.

if (view is ViewGroup)
{
    var g = view as ViewGroup;

    for (var i = 0; i < g.ChildCount; i++)
    {
        SetTypefaceToAllTextViews(g.GetChildAt(i), typeface);
        g.GetChildAt(i).TextAlignment = Android.Views.TextAlignment.Center;
        g.GetChildAt(i).TextDirection = TextDirection.Rtl;

    }
}

The issue is that, I don't have direct access to the gravity of the title.


EDIT #1:

I added this code after casting the view and I can see the font is changing but it won't get centered or any other alignment for some reason

if (view is TextView)
{
    var tv = view as TextView;
    tv.Typeface = typeface;
    tv.TextAlignment = Android.Views.TextAlignment.Center;
    tv.Gravity = GravityFlags.CenterHorizontal;
}

EDIT #2: I was able to add the following to the effect based on an answer below and it somehow centered the element but not exactly at the center. Somehow it's to the right side of the center unless if there are 2 icons in the toolbar on the right and the left side, then, the title will be really centered. Tested on Android 8. Also, I found this issue on a similar solution.

https://github.com/CrossGeeks/CustomNavigationBarSample/issues/3

like image 812
Ali123 Avatar asked Jul 23 '18 08:07

Ali123


1 Answers

I'm not sure how your Effect is implemented, but the following custom renderer seems to work (the bit that is relevant to centering text is in the ToolbarChildAdded method):

public class CustomNavigationPageRenderer : NavigationPageRenderer
{
    Android.Support.V7.Widget.Toolbar toolbar;

    public CustomNavigationPageRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            this.toolbar.ChildViewAdded -= ToolbarChildAdded;
        }

        if (e.NewElement != null)
        {
            var toolbarId = Context.Resources.GetIdentifier("toolbar", "id", Context.PackageName);
            this.toolbar = this.FindViewById(toolbarId) as Android.Support.V7.Widget.Toolbar;
            this.toolbar.ChildViewAdded += ToolbarChildAdded;
        }
    }

    private void ToolbarChildAdded(object sender, ChildViewAddedEventArgs e)
    {
        if (e.Child is Android.Support.V7.Widget.AppCompatTextView tv)
        {
            // identify the title text view and center it
            tv.LayoutParameters = new Android.Support.V7.Widget.Toolbar.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent, (int)GravityFlags.CenterHorizontal);
        }
    }
}

enter image description here

like image 172
gannaway Avatar answered Sep 27 '22 19:09

gannaway