xamarin.formsxamarin.androidxamarin.ioscross-platformtoolbaritems

how to align toolbaritems in xamarin forms for cross platform


so for example

<ContentPage.ToolbarItems>
    <ToolbarItem Name="logo" Icon="businessName.png" Priority="0" Order="Primary"/>
    <ToolbarItem Name="shoppingCartImage" Icon="shopping_cart_IMG.png" Priority="0" Order="Primary"/> Activated="Onclick" />
    <ToolbarItem x:Name="NumberOfItemsInCart" Priority="0" Order="Primary"/>
</ContentPage.ToolbarItems>

will display all items to the right of the toolbar...I would like the logo in the centre and then shopping cart image and label for number in shopping cart to the right...

Read many examples online regarding creating a customer render...(Xamarin forms position of toolbar items for android)

is this still the case? as these Q's were asked years ago...has xamarin been updated to include alignment of items in the toolbar or do I need to create a custom render... please advise thank you


Solution

  • This issue(cannot set the icon to center in xamarin forms code) just happened in Android, you do not need to create a custom renderer in Android.

    First of all, please open the Toolbar.xml file in Resources/layout folder,this is what you set in the MainActivity, which Xamarin.Forms will use to inflate it. Add a ImageView to the android.support.v7.widget.Toolbar like following code.

    <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" >
    
    <ImageView
            android:contentDescription="businessname icon"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/businessname"
            android:layout_gravity="center" />
    </android.support.v7.widget.Toolbar>
    

    Here is running screenshot.

    enter image description here

    Here is a helpful link, you can refer to it.

    https://montemagno.com/xamarin-forms-icons-in-navigation-toolbar/