From 685aed4d83c447baca8eb67dd1c06aa0e6503bfa Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Tue, 24 Apr 2012 16:30:50 -0700 Subject: [PATCH] Added ability to now use a menu in correlation with the Action Bar. We can dynamically remove items and add items to the menu bar based off what you put in the action bar. This allows you to reuse the existing architecture of menus. Target 13 sdk so we always get a target button Added icons and menus for demo. updated OtherActivity.cs to end loop when you use home button by making the intent single top and new task. --- .gitignore | 54 +++- MonoDroid.ActionBar.suo | Bin 40448 -> 40448 bytes MonoDroid.ActionBar/ActionBar/ActionBar.cs | 34 ++- .../ActionBar/MenuItemActionBarAction.cs | 230 ++++++++++++++++++ MonoDroid.ActionBar/HomeActivity.cs | 48 +++- .../MonoDroid.ActionBarSample.csproj | 24 ++ MonoDroid.ActionBar/OtherActivity.cs | 7 +- .../Properties/AndroidManifest.xml | 6 + .../Drawable-hdpi/ic_action_refresh_dark.png | Bin 0 -> 1016 bytes .../Drawable-hdpi/ic_action_search_dark.png | Bin 0 -> 967 bytes .../Drawable-hdpi/ic_menu_refresh.png | Bin 0 -> 3293 bytes .../Drawable-hdpi/ic_menu_search.png | Bin 0 -> 2968 bytes .../Resources/Menu-v11/MainMenu.xml | 24 ++ .../Resources/Menu/MainMenu.xml | 23 ++ .../Resources/Resource.Designer.cs | 107 +++++--- 15 files changed, 506 insertions(+), 51 deletions(-) create mode 100644 MonoDroid.ActionBar/ActionBar/MenuItemActionBarAction.cs create mode 100644 MonoDroid.ActionBar/Properties/AndroidManifest.xml create mode 100644 MonoDroid.ActionBar/Resources/Drawable-hdpi/ic_action_refresh_dark.png create mode 100644 MonoDroid.ActionBar/Resources/Drawable-hdpi/ic_action_search_dark.png create mode 100644 MonoDroid.ActionBar/Resources/Drawable-hdpi/ic_menu_refresh.png create mode 100644 MonoDroid.ActionBar/Resources/Drawable-hdpi/ic_menu_search.png create mode 100644 MonoDroid.ActionBar/Resources/Menu-v11/MainMenu.xml create mode 100644 MonoDroid.ActionBar/Resources/Menu/MainMenu.xml diff --git a/.gitignore b/.gitignore index 9ac0ec3..4d60cba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,50 @@ -# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) -bin -obj +#Library objects +*.lo +*.la -# mstest test results -TestResults \ No newline at end of file +#OS junk files +[Tt]humbs.db +*.DS_Store + +#Visual Studio files +*.[Oo]bj +*.user +*.aps +*.pch +*.vspscc +*.vssscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.[Cc]ache +*.ilk +*.log +*.lib +*.sbr +*.sdf +*.opensdf +*.unsuccessfulbuild +ipch/ +obj/ +[Bb]in +[Dd]ebug*/ +[Rr]elease*/ +Ankh.NoLoad + +#Tooling +_ReSharper*/ +*.resharper +[Tt]est[Rr]esult* + +#Project files +*/[Bb]uild/ + +#Subversion files +.svn + +# Office Temp Files +~$* \ No newline at end of file diff --git a/MonoDroid.ActionBar.suo b/MonoDroid.ActionBar.suo index f5743f1707ee29dae4c37e906d754b6b9e437cfa..e302fc415d176dedc1266ab8d8c4a6ef31fec7b2 100644 GIT binary patch delta 489 zcmZqJ!_=^cX~PR9URwqRhJXM6{|DoVfx?qHm=#!TfP$NKm>n4T0)9_Bk*jo;kr^m* zz+rP0OB=JmvFz6eSMPo7GFRv0`rk(4YbFPB7;JXnXlCYQFajzw2I9#NbQLBWuyAbt z!u`Qe@)uC#HxPsD_zR+e_&tOp_lf2v5FHYqO|=X>xXL7*w4U-c%Tp&yX;aNBx5jT02xz*+aHO*q2j6kpdw+H$e z7BZ7dCWo-tAqi~$G8qyClS`)cfGu9+q{LwbRN;W6iuEUuvuARpw&vtTR!WnPuo#H` a0g4;}Vh~^ix|0cr|7~WR@ttweEEWLsM51m0 delta 205 zcmZqJ!_=^cX~PSq$sd>`CMIx9=3rJ}`S<_-|IIqg4vc&kVlt1bDxPIzW?*1&u-jb4 z(#9;H5w~Nm$&F;6tu;><&t}p&J2{ZUV6y{9GczZn5m1>i5Kn%ft1#Jsg=6y MenuItemsToHide = new List(); + public ActionBar(Context context, IAttributeSet attrs) : base(context, attrs) { @@ -189,6 +195,11 @@ public void AddActions(ActionList actionList) */ public void AddAction(ActionBarAction action, int index) { + //simply put it in the menu items to hide if we are a menu item. + var taskAction = action as MenuItemActionBarAction; + if (taskAction != null) + MenuItemsToHide.Add(taskAction.MenuItemId); + mActionsView.AddView(inflateAction(action), index); } @@ -198,6 +209,7 @@ public void AddAction(ActionBarAction action, int index) public void RemoveAllActions() { mActionsView.RemoveAllViews(); + MenuItemsToHide.Clear(); } /** @@ -207,7 +219,13 @@ public void RemoveAllActions() public void RemoveActionAt(int index) { if (index >= 1) + { + var menuItemAction = mActionsView.GetChildAt(index).Tag as MenuItemActionBarAction; + if (menuItemAction != null) + MenuItemsToHide.Remove(menuItemAction.MenuItemId); + mActionsView.RemoveViewAt(index); + } } public int ActionCount @@ -222,14 +240,22 @@ public int ActionCount * Remove a action from the action bar. * @param action The action to remove */ - public void RemoveAction(ActionBarAction action) { + public void RemoveAction(ActionBarAction action) + { int childCount = mActionsView.ChildCount; - for (int i = 0; i < childCount; i++) { + for (int i = 0; i < childCount; i++) + { View view = mActionsView.GetChildAt(i); - if (view != null) { + if (view != null) + { var tag = view.Tag; - if (tag is ActionBarAction && tag.Equals(action)) + var actionBarAction = tag as ActionBarAction; + if (actionBarAction != null && tag.Equals(action)) { + var menuItemAction = tag as MenuItemActionBarAction; + if (menuItemAction != null) + MenuItemsToHide.Remove(menuItemAction.MenuItemId); + mActionsView.RemoveView(view); } } diff --git a/MonoDroid.ActionBar/ActionBar/MenuItemActionBarAction.cs b/MonoDroid.ActionBar/ActionBar/MenuItemActionBarAction.cs new file mode 100644 index 0000000..8836c52 --- /dev/null +++ b/MonoDroid.ActionBar/ActionBar/MenuItemActionBarAction.cs @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2012 Tomasz Cielecki + * + * Port from https://github.com/johannilsson/android-actionbar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Addition by: Copyright (C) 2012 James Montemagno (motz2k1@oh.rr.com) + */ + +using System; +using System.Collections.Generic; +using System.Text; +using Android.App; +using Android.Content; +using Android.Views; +using Android.Widget; + +namespace MonoDroid.ActionBarSample +{ + + /// + /// This is just a stub so we can set the id. + /// In the long run we should really read in the full menu item and use it. + /// + public class ActionBarMenuItem : IMenuItem + { + public ActionBarMenuItem(int id) + { + m_ItemId = id; + } + + char IMenuItem.AlphabeticShortcut + { + get { throw new NotImplementedException(); } + } + + int IMenuItem.GroupId + { + get { throw new NotImplementedException(); } + } + + bool IMenuItem.HasSubMenu + { + get { throw new NotImplementedException(); } + } + + Android.Graphics.Drawables.Drawable IMenuItem.Icon + { + get { throw new NotImplementedException(); } + } + + Intent IMenuItem.Intent + { + get { throw new NotImplementedException(); } + } + + bool IMenuItem.IsCheckable + { + get { throw new NotImplementedException(); } + } + + bool IMenuItem.IsChecked + { + get { throw new NotImplementedException(); } + } + + bool IMenuItem.IsEnabled + { + get { throw new NotImplementedException(); } + } + + bool IMenuItem.IsVisible + { + get { throw new NotImplementedException(); } + } + + private int m_ItemId; + int IMenuItem.ItemId { get { return m_ItemId; } } + + IContextMenuContextMenuInfo IMenuItem.MenuInfo + { + get { throw new NotImplementedException(); } + } + + char IMenuItem.NumericShortcut + { + get { throw new NotImplementedException(); } + } + + int IMenuItem.Order + { + get { throw new NotImplementedException(); } + } + + IMenuItem IMenuItem.SetAlphabeticShortcut(char alphaChar) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetCheckable(bool checkable) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetChecked(bool @checked) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetEnabled(bool enabled) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetIcon(int iconRes) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetIcon(Android.Graphics.Drawables.Drawable icon) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetIntent(Intent intent) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetNumericShortcut(char numericChar) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetOnMenuItemClickListener(IMenuItemOnMenuItemClickListener menuItemClickListener) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetShortcut(char numericChar, char alphaChar) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetTitle(Java.Lang.ICharSequence title) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetTitle(int title) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetTitleCondensed(Java.Lang.ICharSequence title) + { + throw new NotImplementedException(); + } + + IMenuItem IMenuItem.SetVisible(bool visible) + { + throw new NotImplementedException(); + } + + ISubMenu IMenuItem.SubMenu + { + get { throw new NotImplementedException(); } + } + + Java.Lang.ICharSequence IMenuItem.TitleCondensedFormatted + { + get { throw new NotImplementedException(); } + } + + Java.Lang.ICharSequence IMenuItem.TitleFormatted + { + get { throw new NotImplementedException(); } + } + + IntPtr Android.Runtime.IJavaObject.Handle + { + get { throw new NotImplementedException(); } + } +} + + /// + /// MenuItemActionBarAction will call teh main activitiess "OnOptionsItemSelected", which allows us to re-use code. + /// + class MenuItemActionBarAction: ActionBarAction + { + private Activity m_Activity; + private ActionBarMenuItem m_MenuItem; + public int MenuItemId; + public MenuItemActionBarAction(Context context, Activity activity, int menuId, int drawable) + { + mDrawable = drawable; + mContext = context; + m_Activity = activity; + MenuItemId = menuId; + m_MenuItem = new ActionBarMenuItem(menuId); + } + + public override int GetDrawable() + { + return mDrawable; + } + + public override void PerformAction(View view) + { + try + { + m_Activity.OnOptionsItemSelected(m_MenuItem); + } + catch(Exception ex) + { + } + } + } +} diff --git a/MonoDroid.ActionBar/HomeActivity.cs b/MonoDroid.ActionBar/HomeActivity.cs index 9a0c27b..1eb200d 100644 --- a/MonoDroid.ActionBar/HomeActivity.cs +++ b/MonoDroid.ActionBar/HomeActivity.cs @@ -14,6 +14,8 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * Addition by: Copyright (C) 2012 James Montemagno (motz2k1@oh.rr.com) */ using System; @@ -26,9 +28,10 @@ namespace MonoDroid.ActionBarSample { - [Activity(Label = "Action Bar", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Black.NoTitleBar")] + [Activity(Label = "Action Bar", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon = "@drawable/icon", Theme = "@android:style/Theme.Black.NoTitleBar")] public class HomeActivity : Activity { + private ActionBar m_ActionBar; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); @@ -36,14 +39,19 @@ protected override void OnCreate(Bundle bundle) // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); - ActionBar actionBar = FindViewById(Resource.Id.actionbar); + var actionBar = FindViewById(Resource.Id.actionbar); + m_ActionBar = actionBar; actionBar.SetTitle("BingBong"); ActionBarAction shareAction = new MyActionBarAction(this, createShareIntent(), Resource.Drawable.ic_title_share_default); actionBar.AddAction(shareAction); + ActionBarAction otherAction = new MyActionBarAction(this, new Intent(this, typeof(OtherActivity)), Resource.Drawable.ic_title_export_default); actionBar.AddAction(otherAction); + var searchMenuItemAction = new MenuItemActionBarAction(this, this, Resource.Id.menu_search, Resource.Drawable.ic_action_search_dark); + actionBar.AddAction(searchMenuItemAction); + Button startProgress = FindViewById