I want to handle my custom logo click on ActionBar.
<style name="ReadingsActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/Readings.ActionBar</item>
<item name="logo">@drawable/back</item>
<item name="android:logo">@drawable/back</item>
Style
I must compile on Android 4.4 and here is what I tried:
public override bool OnOptionsItemSelected(Xamarin.ActionbarSherlockBinding.Views.IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
var homeActivity = new Intent(this, typeof(HomeActivity));
StartActivity(homeActivity); ;
break;
}
return base.OnOptionsItemSelected(item);
}
Any solution?
I want to handle my custom logo click on ActionBar.
You can follow the below steps to handle icon click on ActionBar;
Firstly, please make sure you are using Android.Support.V7.App.AppCompatActivity
If not, please install Xamarin.Android.Support.v7.AppCompat
through nuget and inherit your activity from AppCompatActivity
.
Secondly, please define your menu items in Resource\\menu\\actionbar_items.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/action_favorite" android:icon="@drawable/iconName" android:title="favorite" app:showAsAction="ifRoom"/> </menu>
Inflate the menu in OnCreateOptionsMenu
:
public override bool OnCreateOptionsMenu(IMenu menu) { MenuInflater.Inflate(Resource.Menu.actionbar_items, menu); return base.OnCreateOptionsMenu(menu); }
Handle the item click event in OnOptionsItemSelected
:
public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Resource.Id.action_favorite: //do something break; } return base.OnOptionsItemSelected(item); }
Here is the complete Activity:
[Activity(Label = "ActionbarClickDemo", MainLauncher = true,Theme ="@style/AppTheme")]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Android.Support.V7.Widget.Toolbar myToolbar = (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.my_toolbar);
SetSupportActionBar(myToolbar);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.actionbar_items, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.action_favorite:
//do something
break;
}
return base.OnOptionsItemSelected(item);
}
}
And here is the AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
For Creation of ActionBar, please refer to Setting Up the App Bar ;
For Add Items to ActionBar, please refer to Add Views and Action Providers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.