I can create a Toolbar programmatically passing it an ApplicationContext:
Toolbar toolbar = new Toolbar(applicationContext);
But it fails when I try to use it:
Menu menu = toolbar.getMenu();
toolbar.setTitle("");
MenuItem item = menu.add(mContext.getString(R.string.HDLabel));
item.setCheckable(true);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
I get the following exception:
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f01010b a=-1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:482)
at android.widget.TextView.<init>(TextView.java:1043)
at android.widget.TextView.<init>(TextView.java:671)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60)
at android.support.v7.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:72)
at android.support.v7.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:68)
Some others have had the same problem: Upgrading to SDK 21 - Error inflating class android.support.v7.internal.widget.ActionBarContainer.
But I really want to use an ApplicationContext because I want my toolbar to be displayed outside an activity. has anyone been able to do so ?
EDIT: I want my toolbar in a SYSTEM_ALTERT_WINDOW, like the facebook messenger bubble heads described here: What APIs in Android is Facebook using to create Chat Heads?