Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
James Montemagno committed Apr 24, 2012
1 parent cdae2f2 commit 685aed4
Show file tree
Hide file tree
Showing 15 changed files with 506 additions and 51 deletions.
54 changes: 49 additions & 5 deletions .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
#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
~$*
Binary file modified MonoDroid.ActionBar.suo
Binary file not shown.
34 changes: 30 additions & 4 deletions MonoDroid.ActionBar/ActionBar/ActionBar.cs
Expand Up @@ -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.
*
* Changes by: Copyright (C) 2012 James Montemagno (motz2k1@oh.rr.com)
*/

using System;
Expand Down Expand Up @@ -43,6 +45,10 @@ public class ActionBar : RelativeLayout, Android.Views.View.IOnClickListener
private RelativeLayout mHomeLayout;
private ProgressBar mProgress;


//Used to track what we need to hide in the pop up menu.
public List<int> MenuItemsToHide = new List<int>();

public ActionBar(Context context, IAttributeSet attrs)
: base(context, attrs)
{
Expand Down Expand Up @@ -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);
}

Expand All @@ -198,6 +209,7 @@ public void AddAction(ActionBarAction action, int index)
public void RemoveAllActions()
{
mActionsView.RemoveAllViews();
MenuItemsToHide.Clear();
}

/**
Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down
230 changes: 230 additions & 0 deletions MonoDroid.ActionBar/ActionBar/MenuItemActionBarAction.cs
@@ -0,0 +1,230 @@
/*
* Copyright (C) 2012 Tomasz Cielecki <tomasz@ostebaronen.dk>
*
* 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
{

/// <summary>
/// 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.
/// </summary>
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(); }
}
}

/// <summary>
/// MenuItemActionBarAction will call teh main activitiess "OnOptionsItemSelected", which allows us to re-use code.
/// </summary>
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)
{
}
}
}
}

0 comments on commit 685aed4

Please sign in to comment.