“You need to use a Theme.AppCompat theme (or descendant) with the design library” error

Create a ContextThemeWrapper to wrap the Service‘s Context with your custom theme, and get the LayoutInflater from that.

ContextThemeWrapper ctx = new ContextThemeWrapper(this, R.style.TranslucentAppTheme);
tooltipContainer = (CoordinatorLayout) LayoutInflater.from(ctx)
    .inflate(R.layout.tooltip_layout, null);

ContextThemeWrapper modifies the given Context‘s theme with the one you specify in the constructor. Since a Service doesn’t really have a theme, it just tacks yours onto the Service‘s Context, then the LayoutInflater has the appropriate theme to inflate the library Views.


Alternatively, if handling it in the layout XML would be more appropriate or less involved, you might be able set an android:theme attribute on the root <ViewGroup>, which simply causes the LayoutInflater to do the Context wrapping internally. For example:

<android.support.design.widget.CoordinatorLayout
    ...
    android:theme="@style/TranslucentAppTheme">

However, this will only work with the platform LayoutInflater starting with Lollipop (API level 21). The support/androidx libraries are able to handle that attribute on older versions, but the way it’s set up is intended for use in Activity classes only, and it’s likely simpler to just do the wrapping yourself in that case.

Leave a Comment