Hi I am developing an Android application. In my application I am using Sherlock action. I've defined few menu items in action-bar like in following manner
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/card_menu" android:actionLayout="@layout/action_button" android:showAsAction="always" android:title="cards"> <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:actionLayout="@layout/notification_icon" android:icon="@drawable/notification" android:showAsAction="always" android:title="Notifications"/> <item android:id="@+id/filter" android:icon="@drawable/filter" android:showAsAction="always" android:title="Filter"/> </menu>
and My action_button looks like :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/menu_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/spinner_ab_focused_maroon"/> <TextView android:id="@+id/menu_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView0" android:text="C1"/> </RelativeLayout>
Now everything is displayed but my problem is that when I click on card_menu item
where I define sub menus and also define action layout. It's not showing those sub menus. My other menu items are working properly. Only when I define action layout for my item which contains sub menus that I am not able to display sub-menu. If I remove action layout it works fine.
I know if we define action layout for item then we have to manually handle click listener. I did that in following manner
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 to click event for that item but not able to show drop-down sub menu items.
How do I solve this problem?
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
And by app:actionLayout he means the name of your app package. So if your app package name is twitter it would be twitter:actionLayout .
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with MenuInflater . For a guide to using menus, see the Menus developer guide. file location: res/menu/filename.xml. The filename will be used as the resource ID.
Try this code in your activity.
Be sure to properly set your
R.menu.menuidentifier
R.id.menuitemidentifier
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbarhelpmenu, menu); final Menu m = menu; final MenuItem item = menu.findItem(R.id.ActionConnection); item.getActionView().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { m.performIdentifierAction(item.getItemId(), 0); } }); return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With