0

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

John
  • 3,965
  • 21
  • 77
  • 163

1 Answers1

1

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/

Leon
  • 8,404
  • 2
  • 9
  • 52
  • ahh thats brilliant thanks for the help...only thing is tho my audience is going to be ios and andriod...so maybe in the long run it would be more feasible to create the custom render...more work now but as I say I need it for ios also – John Jun 13 '20 at 13:32
  • You can see link in the answer, it have solution about iOS – Leon Jun 13 '20 at 13:36
  • If the answer is helpful, please accept it as answer, it will help others who have similar issues – Leon Jun 13 '20 at 13:37
  • If you used custom renderer,you need to get the action bar then create a imageview, set the imageView to the action bar and set the layout in the center. I have tried that way, but the action bar always get null, we need set a new action in the main activity, that’s too complex. – Leon Jun 13 '20 at 13:48
  • thats brilliant thanks for your help. got both in the link – John Jun 14 '20 at 14:41