Xamarin Forms - Android - Set Toolbar Title to Center

I am developing Xamarin forms app.
In Android, I know, the toolbar title is in left by default.

I need it to be in center aligned like iOS.

Below is my code which I tried:

Toolbar.axml: in layout folder

    <?xml version="1.0" encoding="UTF-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toolbar"
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
       android:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/tabIcon"
            android:layout_width="wrap_content" 
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:paddingTop="2dp"
            android:background="@android:color/holo_red_dark"/>
    </android.support.v7.widget.Toolbar>

Android Custom Renderer class:

[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]

namespace Assure.Droid
{
    public class CustomPageRenderer : PageRenderer
    {
        global::Android.Views.View view;
        Activity activity;

        public CustomPageRenderer(Context context) : base(context)
        {
        }
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            { 
                return;
            }

            try
            {
        activity = this.Context as Activity;
                view = activity.LayoutInflater.Inflate(Resource.Layout.Toolbar, this, false);
                var imageView = view.FindViewById<TextView>(Resource.Id.tabIcon);
                imageView.Text = page.NewElement.Title;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message);
            }
        }
     }
}

I have stuck in my deliverables. Please kindly help me guys. :)

Sign In or Register to comment.