0
votes

I am trying to show search view on navigation bar in my xamarin.forms project for Android. But the search toolbar item (search icon) is not visible on android 7.0 and above which works fine on android 6.0.

I have followed this link to write custom renderer for a content page. It works fine on android 6.0.

On searching further, I got this link which is working fine if I create xamarin.android project but the same is not working on xamarin.forms.

Here is my MainActivity code:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static Android.Support.V7.Widget.Toolbar ToolBar { get; private set; }

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        ToolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        return base.OnCreateOptionsMenu(menu);
    }
}
1

1 Answers

1
votes

Not know why this custom method not working any more in later version.But In Xamarin forms , there is a easy way to show SearchBar in NavigationBar.Maybe you can have a try this.

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Samples.Views.MainPage" Title="Samples">
    <NavigationPage.TitleView>
        <SearchBar Text="input here"></SearchBar>
    </NavigationPage.TitleView>

    <StackLayout>
        <Button Text="SearchPage Sample" VerticalOptions="Center" Clicked="SearchPageSample_Clicked"/>
    </StackLayout>
</ContentPage>

enter image description here