AppCompatActivity, Theme.AppCompat, and Slider Not Compatible

Nathan Sokalski 4,126 Reputation points
2022-10-26T02:07:12.983+00:00

I have an Activity that inherits from AppCompatActivity. The app uses a com.google.android.material.slider.Slider. Until now I was using a theme (the parent of AppTheme in my styles.xml file) inheriting from Theme.MaterialComponents.Light.NoActionBar. But then some of the style(s) I had previously created stopped displaying correctly, and I found the following page:

https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity

This page says:

Note that every activity that extends this class has to be themed with Theme.AppCompat or a theme that extends that theme.

So I changed my theme to Theme.AppCompat.Light.NoActionBar. However, this prevented the Slider from working (I'm assuming because the Slider requires the MaterialComponents). It seems as if there are some things requiring AppCompat and others requiring MaterialComponents, but they don't seem to be compatible with each other. What should I do?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,936 Reputation points Microsoft Vendor
    2022-10-27T07:30:57.933+00:00

    Hello,

    Please use Theme.MaterialComponents.Light.NoActionBar to your app default theme, then create a custom theme that extend the Theme.AppCompat.Light.NoActionBar in your styles.xml like following code.

       <style name="AppTheme" parent="Theme.MaterialComponents.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="MyCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">  
       <!-- Customize your theme here. -->  
               <item name="colorPrimary">#ff0000</item>  
               <item name="colorPrimaryDark">#ff0000</item>  
               <item name="colorAccent">#ff0000</item>  
                 
           </style>  
    

    If I have a button that need to set the MyCustomTheme, I can use following theme.

       <Button  
                android:layout_column="0" android:layout_row="4"  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:theme="@style/MyCustomTheme"  
               android:text="click"/>  
    

    You can refer to this document about android Material themes

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful