I just can't get the ActionProvider to show a submenu and I don't undestand why. I have my menu defined in xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/dossier_menu"
android:showAsAction="always"
android:actionProviderClass="com.some.other.mockup.MActionProvider">
</item>
</menu>
action provider class:
public class MActionProvider extends ActionProvider {
private static final String TAG = "MActionProvider";
private static final int LIST_LENGTH = 3;
private Context context;
/**
* Creates a new instance.
*
* @param context Context for accessing resources.
*/
public MezzActionProvider(Context context) {
super(context);
this.context = context;
}
@Override
public View onCreateActionView() {
View view = View.inflate(context, R.layout.action_layout, null);
//view must be returned not tetxView or just buttonView
return view;
}
@Override
public boolean hasSubMenu() {
Log.d(TAG, "hasSubMenu");
return true;
}
@Override
public boolean onPerformDefaultAction() {
Log.d(TAG, "onPerformDefaultAction");
return super.onPerformDefaultAction();
}
/**add submenu in code**/
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
Log.d(TAG, "onPrepareSubMenu");
subMenu.clear();
subMenu.add(Menu.NONE, Menu.NONE, 1,"Mezz 1");
subMenu.add(Menu.NONE, Menu.NONE, 2, "Mezz 2");
}
}
and action layout is:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mezz State ++"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="4dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="@dimen/buttonHeight"
android:src="@android:drawable/ic_menu_add"
android:layout_alignParentLeft="true"
android:id="@+id/img1"
android:layout_below="@id/textView"/>
I tried using button instead of image view, even tried adding submenu to the menu item in xml, when I click on the action provider it just doesn't show any submenu I can't figure out why.
Thanks
I have found the answer here:
Can't display sub-menu for custom ActionProvider
Problem is my onCreateActionView() method doesn't return null.
So thanks to Jason Robinson answer here is how:
and submenu.xml contains: