Material-components-android: [Slider] Material theme needs to be explicitly defined to render Slider in preview mode

Created on 25 Mar 2020  路  5Comments  路  Source: material-components/material-components-android

Description:
Without explicitly specifying a theme, the Slider class fails to instantiate in the layout preview mode. The activity inherits from AppTheme, AppTheme inherits from Theme.MaterialComponents.Light.NoActionBar.

It builds and runs fine on the device, just that in the preview mode it never gets rendered.

java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant). 聽聽at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)

Expected behavior: Renders in preview mode, doesn't require extra theme

Source code:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

  <com.google.android.material.slider.Slider
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:value="20"
            android:valueFrom="5.0"
            android:valueTo="100.0"
            app:labelBehavior="gone"
            app:trackColorActive="@color/icon_primary"
            app:trackColorInactive="@color/ui_dark"/>

To fix this, I had to add android:theme="@style/AppTheme" to the xml.

Android API version: 29

Material Library version: 1.2.0-alpha05

Device: API28 Samsung A70

bug studio

Most helpful comment

Looks like it! I was able to repro the issue in 4.0 Beta 3. I can ask the team if it's possible to backport the fix to 3.6 and 4.0. Thanks for investigating!

All 5 comments

Thanks for filing this! What version of Android Studio did this occur on?

I tried it out on the latest stable, so 3.6 something, but I can try it out tomorrow with the 4.x canary.

Awesome thanks! If you could repro it on the latest Canary that would be great as well.

Just tried it out with the newest Canary version and it works just fine. So it's probably some Android Studio problem then which was already solved?

Android Studio 4.1 Canary 4
Build #AI-193.6494.35.41.6325121, built on March 23, 2020
Runtime version: 1.8.0_242-release-1644-b3-6222593 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 12
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: org.jetbrains.kotlin

Looks like it! I was able to repro the issue in 4.0 Beta 3. I can ask the team if it's possible to backport the fix to 3.6 and 4.0. Thanks for investigating!

Was this page helpful?
0 / 5 - 0 ratings