MaterialCardview requires Theme.AppCompat - android

I'm trying to test my skills on new Google Material components.
But for now I am encountering a problem with MaterialCardView
The building process tells me
The style on this component requires your app theme to be Theme.AppCompat
[..]
at com.google.android.material.card.MaterialCardView.<init>
With this clue I added
style="#style/Theme.AppCompat" & android:theme="#style/Theme.AppCompat" to the MaterialCardView and also to my Activity in the manifest.
I tried also to change my Acitivity to AppCompatActivity but without any success.
I also tried to set styles told by material.io documentation but without success !
Have you some clues?
Thanks

According to Material Components 1.2.1 you need to do this:
Depend on the library implementation 'com.google.android.material:material:1.2.1'
You'll need to have compileSdkVersion 30
Your activity needs to extend AppCompatActivity (or use AppCompatDelegate)
You have to use a Material Components theme
Source: https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md
The easiest way to get started is for your current theme to extend Theme.MaterialComponents.*.Bridge instead of Theme.AppCompat.*.
Additionally you'll need to override the following attribute in your theme, otherwise the card color will be broken:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<item name="elevationOverlayEnabled">false</item>
</style>
Don't set android:theme="#style/Theme.MaterialComponents" on the card. You'll lose color information (primary, secondary, accent,...) from your theme on every widget inside the card.
Don't set style="#style/Theme.MaterialComponents on the card. Don't mix themes and styles.

Related

How to apply MaterialComponents only to some widget?

I am migrating from AppCompat to MaterialComponents, and I would like to keep some widgets (like the Bottom Navigation Bar) AppCompat-themed. However, when I want to apply MaterialComponents theming to Buttons and TextFields, I must set my app theme to MaterialComponents.... and therefore all my widgets are MaterialComponents-themed. How can I make only some widgets MaterialComponents-themed ? I have been looking for an answer on StackOverflow but couldn't find anything.
You can use Bridge themes like :
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
Bridge themes inherit from AppCompat themes, but also define the new Material Components theme attributes for you. If you use a bridge theme, you can start using Material Design components without changing your app theme. (From documentaion)
Have a look : Get started - Material Design

How to customize chip style when my app theme inherit from "Theme.AppCompat"?

I want to use the android widget "ChipGroup & Chip" in my app which are from android material 'com.google.android.material', but I am using like principle theme for My App "Theme.AppCompat" so when I try to custom the style for each of theme I always get a crash saying:
This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
How should I proceed?

BoxInsetLayout on Android wearable round screen not working with Theme.AppCompat

I'm developing an app for Android wearables. I want to create a layout working on both round and square screens. Therefore I'm using BoxInsetLayout.
I also want to use a CheckBox from Material Theme. Therefore I'm using a custom theme derived from Theme.AppCompat.Light.
<style name="AppTheme" parent="#style/Theme.AppCompat.Light">
The problem is that BoxInsetLayout is not working properly on round screens. I'm having the same issue as described here but the solution mentioned there is not working for me. The relevant code parts are the same except the theming part.
When I switch the theme to Theme.DeviceDefault I only get the CheckBox from Holo.
Try adding:
<item name="android:windowOverscan">true</item>
to your theme. It is necessary for dispatching insets.

Failure to change Theme in Android Studio

I want to change the Theme to Theme.Holo, but it was crashed when I run the apps. I only made change in Manifest.xml as below, and so far there are no error message shown.
Original:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
Changed:
<style name="AppTheme_Holo" parent="android:style/Theme.Holo"></style>
Here is the logcat:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
logcat message seem to tell me I can only use the Theme.AppCompat, and I think this theme is coming from supporting library appcompat v7:22.
I have tried to search around the link as below from Android developer site, but seem the setting above has no difference with their suggestion
May I know why I can only use the Theme from the supporting library v7:22?
Is there something I missed to change?
Android Developer site:Styling the Action Bar
Yes there is something more. Your activity is extending AppCompatActivity or ActionBarActivity and that needs a Theme.AppCompat theme or any descendant. You just need to change that to Activity or FragmentActivity from support library v4.
You might be using ActionBarActivity which uses Theme.AppCompat.*. Make your activity extend android.app.Activity to get rid of this error.

Android - apply theme from older API

I would like to deploy my app on APIs 8-17. However, for purely aesthetic reasons I would like to apply the default theme as it appears on api 8 as the theme for the app across all API levels.
For example, the older theme has an edittext that has an orangeish border around it, whereas the newer them uses a borderless blue line.
By limiting which APIs i deploy too I have been able to accomplish this but that isn't really a solution.
Does anyone know how this can be accomplished?
Thanks
Update
For whatever reason applying "Theme" as the theme did not force it to revert to the "Theme" theme, but instead left it as the default Holo. Using the answers below I simply called "Theme" as the parent in my custom theme (without altering any of its attributes) and set it as my application theme in the manifest. This solved it.
In your res/values directory, you can have a themes.xml file with:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="#android:Theme">
</style>
</resources>
Your app theme will now subclass from default Theme instead of Theme.Holo and you should be able to get older theme on newer android versions as well.
If you're using the default theme, it will be different between the API levels. However in the styles, you can create a custom Theme, modify an existing Theme or give a different Theme to each different API of your choice.

Categories

Resources