0

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?

Community
  • 1
  • 1
mbonnin
  • 6,893
  • 3
  • 39
  • 55
  • post your code please – Kostas Drak Mar 02 '16 at 19:45
  • Never use app context to work with views. Views are tied to an activity, use an activity context. – Eugen Pechanec Mar 02 '16 at 19:50
  • **Where outside an activity would you like the toolbar to be displayed?** If that's even possible it's some high level stuff and considering your question you're more likely misunderstanding how a part of Android works. So let's find out. – Eugen Pechanec Mar 02 '16 at 19:51
  • I'm trying to get it in a SYSTEM_ALTERT_WINDOW, see my edit. The toolbar will be part of a bigger ViewGroup that resides in my Activity but that I want to detach and keep in a Service when needed. Everything works well except the toolbar part. – mbonnin Mar 02 '16 at 19:54
  • post the coode or an image of that `ViewGroup` – Kostas Drak Mar 02 '16 at 19:55
  • That sort of caching is useless. Just recreate it everytime the activity is recreated. If you want it hidden, just set it's visibility to gone. If for some reason it's not enough, detach it, keep it in a field variable (not static, please...) and reattach it later. Whatever you're trying to do now a) is unnecessarily complicated b) will cause you trouble in the future. Keep it simple. – Eugen Pechanec Mar 02 '16 at 20:00
  • @EugenPechanec It's not caching. The service will actually display the ViewGroup on top of other activities. I'm trying to accomplish something similar to ios9 Picture-in-picture: http://appleinsider.com/articles/15/09/18/inside-ios-9-apples-new-ipad-only-picture-in-picture-mode-lets-you-keep-watching-video-with-any-task – mbonnin Mar 02 '16 at 20:37

2 Answers2

1

You can try wrapping your app context in a ContextThemeWrapper to decorate the bare app context with a theme that extends from AppCompat like so:

Context context = new ContextThemeWrapper(getApplicationContext(), R.style.AppTheme);

This context will provide all the AppCompat attributes so you can create widgets programmatically.

If you want to use AppCompat widgets you'll have to create them manually, e.g. new AppCompatImageView(...) instead of new ImageView(...).

This context also does not provide AppCompat layout inflater so framework widgets will not be replaced by their AppCompat counterparts automatically upon inflation.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

Show your class. Will try to figure it out.

try this method to get contextgetApplicationContext() or activityName.this.

it will be better if you show some code.

Aamir Ali
  • 203
  • 1
  • 8