I am trying to show different menus in actionbar (I’m using the sherlock action bar), depending on the javascript function that performs open page in webview.
For example, in page1.html, Android.showMenu(1) is called.
While in page2.html, Android.ShowMenuB(2) is called.
Currently the menu is all defined by the java code, like this:
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu (menu);
MenuItem menu0 menu.add = (0, 0, 0, "ButtonA");
{
menu0.setIcon (R.drawable.buttona);
menu0.setShowAsAction (MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem menu1 menu.add = (0, 1, 1, "ButtonB");
{
menu1.setIcon (R.drawable.buttonb);
menu1.setShowAsAction (MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return true;
}
I’m detecting the presence of the functions using the JavascriptInterface this way:
myWebView.addJavascriptInterface (new WebAppInterface (this), "Android");
...
{public class WebAppInterface
Context mContext;
WebAppInterface (Context c) {
mContext = c;
}
@ JavascriptInterface
public void showMenuA (int menu) {
??
}
}
Is it possible? Or will I have to use other methods?
You can use
onPrepareOptionsMenu()to populate action items on the fly and invalidate the action bar usinginvalidateOptionsMenu()