6
votes

Hi I am developing an android application. In my application I am using ActionBarSherlock. I defined few menu items in the action-bar, in the following way:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/card_menu"
     android:title="cards" 
     android:showAsAction="always"
     android:actionLayout="@layout/action_button"
  >
         <menu>
             <item android:id="@+id/C1"
              android:title="C1" />
             <item android:id="@+id/C2"
              android:title="c2" />
             <item android:id="@+id/C3"
               android:title="C3" />
         </menu>
 </item>
<item android:id="@+id/notification"
      android:title="Notifications"
      android:showAsAction="always"
      android:actionLayout="@layout/notification_icon"
      android:icon="@drawable/notification"
 />

<item android:id="@+id/filter"
      android:icon="@drawable/filter"
      android:title="Filter" 
      android:showAsAction="always"
 />

Now, everything displayed very well, but my problem is that when I click on a card_menu item where I define sub menus and also define an action layout; it's not showing those sub menus.
My other menu items are working properly. Only when I define an action layout for my item which contains sub menus at that time I am not able to display the sub menu.
If I remove the action layout, then it's working fine...

I know if we define an action layout for an item, then we have to manually handle the click listener. I did that the following way:

final MenuItem item = menu.findItem(R.id.card_menu);
        item.getActionView().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onOptionsItemSelected(item);
            Toast.makeText(getActivity(), "click on menu", Toast.LENGTH_SHORT).show();
        }
        });

I am able to handle the click event for that item, but not able to show drop-down sub menu items..

How to solve this problem? How can I open my sub menus?

Need Help.... Thank you...

1
did you find a solution for this?Nil

1 Answers

0
votes

I had similar issue and solved it with a trick using a Spinner inside the Actionbar. So my layout of the actionbar in res/menu is (action_share is important):

<item android:id="@+id/action_share"
    android:showAsAction="always"
    android:actionLayout="@layout/actionbar_spinner_export" />

In my res/layout folder, I put in actionbar_spinner_export:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sp_export"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/actionbar_item_selector" />

Then in the onCreateOptionsMenu, I got this spinner and add a custom ArrayAdapter to this. I can only post the code in c#, since I am working with Xamarin.Android Cross Platform Development. But its nearly the same for Java:

IMenuItem spinnerExport = menu.FindItem(Resource.Id.action_share);
        _sp_export = spinnerExport.ActionView.FindViewById<Spinner>(Resource.Id.sp_export);
        _sp_export.Adapter = new ExportAdapter(_parent, Resource.Layout.actionbar_export_row, new List<String>{"Drucken", "Als PDF", "Als Text", "Als Tabelle"});

Then in my custom ExportAdapter, I put in getView the image, I want to show (in this case a share-icon). And in getDropDownView, I put all my items. Here is the code:

class ExportAdapter : ArrayAdapter
{
    private List<String> _objects = null;
    private Context _context = null;

    public ExportAdapter(Context context, int resourceId, List<String> objects) : base(context, resourceId)
    {
        _context = context;
        _objects = objects;
    }

    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        convertView = ((Activity) _context).LayoutInflater.Inflate(Resource.Layout.actionbar_export_row, parent, false);

        TextView tv_text = convertView.FindViewById<TextView>(Resource.Id.tv_text);
        ImageView iv_image = convertView.FindViewById<ImageView>(Resource.Id.iv_image);

        RelativeLayout.LayoutParams lp_iv = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
        lp_iv.AddRule(LayoutRules.CenterInParent);
        iv_image.LayoutParameters = lp_iv;
        iv_image.SetPadding(5,5,5,5);

        tv_text.Visibility = ViewStates.Gone;
        iv_image.SetImageResource(Resource.Drawable.ic_action_share);

        return convertView;
    }

    public override View GetDropDownView (int position, View convertView, ViewGroup parent)
    {
        convertView = ((Activity) _context).LayoutInflater.Inflate(Resource.Layout.actionbar_export_row, parent, false);

        TextView tv_text= convertView.FindViewById<TextView>(Resource.Id.tv_text);
        tv_text.Text = _objects.ElementAt(position);

        return convertView;
    }

    public override int Count {
        get {
            return _objects.Count();
        }
    }

    public String getItemAtIndex(int position)
    {
        return _objects.ElementAt(position);
    }
}

This way, I have an actionbar Icon, which looks like a real actionbar-item. And when I click on it, there will be the items in my spinner showing up.