Reputation: 1619
I'm getting "You need to use a Theme.AppCompat theme (or descendant) with the design library" error every time even if I'm obviously using an AppCompat Theme (a descendant one).
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'm inflating the layout inside a service:
tooltipContainer = (CoordinatorLayout) LayoutInflater.from(this).inflate(R.layout.tooltip_layout, null);
Upvotes: 9
Views: 11576
Reputation: 39191
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 View
s.
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.
Upvotes: 37
Reputation: 1692
I think Mike M solution is the right one if you're getting the error from a service, which is how the original question is stated. I think Yevhen is talking about getting this problem that is not originating from a service. It's to that case which, which I'll share my solution.
I was getting the same error stating that I needed to use a Theme.AppCompat theme. In my case it was from a custom dialog that contained 6 NumberPicker widgets. That error would occur 6 times (one for each widget) whenever I opened that dialog. Here's what my styles orginally looked like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And, this is what I did to correct the problem:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Notice that I didn't have AppTheme.NoActionBar deriving from an Theme.AppCompat theme.
Upvotes: 1
Reputation: 801
Also receive such error in logcat:
"E/ThemeUtils: View class TableCircleCustomView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant)."
My app theme is "Theme.AppCompat.Light.NoActionBar"
Here is my custom view class:
class TableCircleCustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
....
}
Then in Fragment i'm adding this object to RelativeLayout like so
val viewItem = TableCircleCustomView(context)
with(RelativeLayout.LayoutParams(objectWidth, objectHeight)) {
leftMargin = objectPosX
topMargin = objectPosY
binding.restaurantMap.addView(viewItem, this);
}
Can somebody help to solve it. Have no idea for what element i should set theme and how ((
Upvotes: -2