After so much research i came to this discovery of code that i can use to populate the toolbar with icons. I have two icons on my Toolbar,A three doted one and a search icon, The functionality of one has been generated by the IDE and it works fine, am yet to figure out how to program the search icon to request keyboard focus for an EditText in the same toolbar... My code is here below
//Code to populate the toolbar with icons
public override bool OnCreateOptionsMenu(IMenu menu)
{
//This one inflates a three dotted menu to the rightmost part of the toolbar
MenuInflater.Inflate(Resource.Menu.menu2, menu);
//This one adds a search icon next to the Three dotted menu
MenuInflater.Inflate(Resource.Menu.menu4, menu);
return true;
}
//This method handles the functionality of the three dot menu icon by generating the associated menu
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
Intent intent = new Intent(this, typeof(SecondActivity));
StartActivity(intent);
OverridePendingTransition(Resource.Animation.slide_in_right, Resource.Animation.abc_popup_exit);
return true;
}
return base.OnOptionsItemSelected(item);
}
//I need code here to make the search icon do something, some kind of inbuilt method or something thanks
How to program other icons added to the toolbar using inbuilt methods or any other way will surely be appreciated thanks...
You could try to create a menu.xml
and set the search item like below:
menu.xml:
<?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/menu_search"
android:icon="@drawable/fiveplus"
android:title="search"
app:showAsAction="collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
then in your activity:
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu, menu);
searchMenuItem = menu.FindItem(Resource.Id.menu_search);
Android.Support.V7.Widget.SearchView searchView = (Android.Support.V7.Widget.SearchView)searchMenuItem.ActionView;
searchView.QueryTextChange += SearchView_QueryTextChange;
return true;
}
private void SearchView_QueryTextChange(object sender, Android.Support.V7.Widget.SearchView.QueryTextChangeEventArgs e)
{
//do something you want
Toast.MakeText(this, e.NewText,ToastLength.Short).Show();
}