Skip to content

Basic implementation

Federico Navarrete edited this page Jul 5, 2019 · 13 revisions


Step 1. Download the package from NuGet:

NuGet Badge Xamarin-MaterialSearchBar


Step 2. Define your control in your XML

<tk.supernovaic.MaterialSearchBar.MaterialSearchBar
	style="@style/MaterialSearchBarLight"
	app:mt_speechMode="true"
	app:mt_hint="Custom hint"
	app:mt_maxSuggestionsCount="10"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:id="@+id/searchBar" />

Step 3. Define your Activity

Edit the Activity (AppCompatActivity) where you are working and add the following parts:

  • MaterialSearchBar.IOnSearchActionListener interface for reading the buttons actions in the Bar.
  • Create an object of the type MaterialSearchBar.
  • Create a listener for listening your Bar.

Example code:

public partial class YourClassActivity : AppCompatActivity, MaterialSearchBar.IOnSearchActionListener
{    
	private MaterialSearchBar MSearchBar { get; set; }

	protected override void OnCreate(Bundle savedInstanceState)
	{
		base.OnCreate(savedInstanceState);

		MSearchBar = FindViewById<MaterialSearchBar>(Resource.Id.searchBar);

		MSearchBar.SetOnSearchActionListener(this);

		MSearchBar.AddTextChangeListener(new MaterialSearchBarListener());
	}

	void MaterialSearchBar.IOnSearchActionListener.OnButtonClicked(int p0)
	{
		switch (p0)
		{
			case MaterialSearchBar.ButtonNavigation:
				Drawer.OpenDrawer((int)GravityFlags.Left);
				break;
			case MaterialSearchBar.ButtonSpeech:
				break;
			case MaterialSearchBar.ButtonBack:
				MSearchBar.DisableSearch();
				break;
		}
	}
}

public partial class YourClassActivity
{
	private class MaterialSearchBarListener : Java.Lang.Object, ITextWatcher
	{
		public void AfterTextChanged(IEditable s)
		{

		}

		public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
		{

		}

		public void OnTextChanged(ICharSequence s, int start, int before, int count)
		{

		}
	}
}