Santyy
Santyy

Reputation: 176

How to access tap or click of an toolbar image in android xamarin forms

I have placed an image inside toolbar like in the below code. I did this to have an toolbar icon on the left side.

<?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">
    <ImageView
        android:id="@+id/homeicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/appicon"
        android:scaleType="fitStart" />
</android.support.v7.widget.Toolbar>

I need to access the tap or click of the image in Activity.cs. It would be grateful, if you have any other way to place Toolbar icon on the leftside in xamarin forms.

Upvotes: 2

Views: 396

Answers (1)

York Shen
York Shen

Reputation: 9084

I am looking for a way to place toolbaritem on the left side of the toolbar in xamarin forms application

You could refer to my answer : Xamarin.forms toolbar Android .

Place ToolbarItem on the left side of the toolbar in Xamarin.Forms, put this code in yourPage.xaml :

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Share"  Activated="EditClicked_Share"   Order="Primary" Priority="0" />
    <ToolbarItem Text="Create" Activated="EditClicked_Create"  Order="Primary" Priority="1" />
    <ToolbarItem Text="Save"   Activated="EditClicked_Save"    Order="Primary" Priority="2" Icon="ic_action_content_save.png" />
</ContentPage.ToolbarItems>

I need to access the tap or click of the image

In yourPage.cs :

private void EditClicked_Save(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Picture Clicked");
}

Update :

This isn't currently supported in Forms, you could write a Custom Renderer that overrides the base Android Toolbar renderer.

Herer is a simple solution, create a picture which color is same with the Toolbar, in my project, my picture is blue.png :

enter image description here

Place it left side of the Toolbar, add some blank ToolbarItem between the picture and your ToolbarItem to adjust the space.

<ContentPage.ToolbarItems>
    <ToolbarItem  Activated="EditClicked_Save" Order="Primary" Priority="0" Icon="ic_action_content_save"/>
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="1" Icon="blue"/>
</ContentPage.ToolbarItems>

Effect :

enter image description here

Upvotes: 2

Related Questions