"You must use Theme.AppCompat (or child) theme with the" error "library

I get "You need to use the Theme.AppCompat (or descendant) theme with a design library error every time, even if I obviously use the AppCompat (descendants) theme.

Dependencies:

compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:design:23.3.0' compile 'com.android.support:support-v4:23.3.0' 

layout:

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tooltip_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"> <ImageView android:id="@+id/tooltip_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_delete_image" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_anchor="@id/tooltip_image" app:layout_anchorGravity="top|end"/> </android.support.design.widget.CoordinatorLayout> 

Theme:

 <style name="TranslucentAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style> 

manifest:

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/TranslucentAppTheme"> <activity android:name=".MainActivity"> (...) </activity> 

I inflate the layout inside the service :

 tooltipContainer = (CoordinatorLayout) LayoutInflater.from(this).inflate(R.layout.tooltip_layout, null); 
+5
source share
1 answer

Create a ContextThemeWrapper to wrap the Service Context your custom theme and get a LayoutInflater from it.

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

ContextThemeWrapper changes the specified Context theme from the one you specified in the constructor. Since a Service doesn’t really have a theme, it just binds you to the Service Context , then LayoutInflater has an appropriate theme for inflating View s design.

+20
source

All Articles