Yura Babiy
Yura Babiy

Reputation: 525

Xamarin.Forms Search Widget is not showing

I tried to use Search Widget in my Xamarin.Forms project as people doing it in Xamarin.Android but it is not working. I want the same result as you see on image below.enter image description here Here is my code:

**Main Activity :**
using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using Android.Support.V4.App;
using Android.Support.V4.Widget;
using Android.Util;
using Android.Support.V4.Content;


namespace GitRemote.Droid
{
    [Activity(Label = "GitRemote", MainLauncher = true, ConfigurationChanges =     ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : FormsAppCompatActivity
    {

    protected override void OnCreate(Bundle bundle)
    {
        FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
        FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
        base.OnCreate(bundle);
        Forms.Init(this, bundle);
        LoadApplication(new App());

    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        Android.Widget.SearchView searchView;
        MenuInflater.Inflate(Resource.Menu.menu_home, menu);

        var searchItem = menu.FindItem(Resource.Id.action_search);
        var provider = MenuItemCompat.GetActionView(searchItem);
        searchView = provider.JavaCast<Android.Widget.SearchView>();
       // searchView.SetIconifiedByDefault(false);
        searchView.QueryTextSubmit += (sender, args) =>
        {
            Toast.MakeText(this, "You searched: " + args.Query, ToastLength.Short).Show();
        };
        //searchView.QueryTextSubmit += (sender, args) =>
        //{

        //    var view = sender as Android.Support.V7.Widget.SearchView;
        //    if ( view != null )
        //        view.ClearFocus();
        //};
        //return base.OnCreateOptionsMenu(menu);
        return true;
    }
}
}

**menu_home:**
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

  <item android:id="@+id/action_search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always|withText"
        app:actionViewClass="android.widget.SearchView"/>

</menu>

Upvotes: 0

Views: 837

Answers (1)

Calin Vlasin
Calin Vlasin

Reputation: 1433

I had the same problem with a Xamarin.Forms Search Component.

Xamrin.Forms has a control that is called: SearchBar. Indeed... on the Emulator is shown but when you install the application on an android device(eg. Samsung S8) the SearchBar is not Visible.

I can sugest a workaround: Set the Height of the searchbar manually. 42 is the default for SearchBar.

HeightRequest="42"

The issue was addressed here https://bugzilla.xamarin.com/show_bug.cgi?id=43975

Upvotes: 2

Related Questions