?android:attr/colorPrimary vs ?attr/colorPrimary - android

Could somebody please tell me what the difference is between
?android:attr/colorPrimary
and
?attr/colorPrimary
Whichever i use, result is the same. Altough first option causes android.view.InflateException on some devices.

Both almost works the same often. when you use ?attr/colorPrimary, its works totally fine as compiler already knows that 'android' has to be appended.
And regarding you saying that ?android:attr/colorPrimary gives you exception then in that case, try using the second option only ..
For Example in your styles.xml : The following may/may not work everytime
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:colorPrimary">#color/primary_material_dark</item>
<item name="android:colorPrimaryDark">#color/primary_dark_material_dark</item>
</style>
</resources>
But this majorly works :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary_material_dark</item>
<item name="colorPrimaryDark">#color/primary_dark_material_dark</item>
</style>

The builder knows that the color comes from attr so it's redundant. The documentation states:
... you do not need to explicitly state the type ... you can exclude the attr type.
See: App resources overview: Referencing style attributes
As for android: - the prefix in android:colorPrimary refers to an attribute of the material theme (API 21 and later). Without the prefix, ?colorPrimary will be taken from the support library.

Related

java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute

I have a com.google.android.material.button.MaterialButton component in one of my layout file and I get this error when I am using the latest version of the Material Components library (com.google.android.material:material:1.0.0-alpha3):
java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute.
It wasn't present in 1.0.0-alpha1. Is this a bug in the library or should I just specify a textAppearance attribute from now?
Does your theme extend from Theme.MaterialComponents? More info about how to ensure all the components will work correctly can be found at https://material.io/develop/android/docs/getting-started/
If you are using any of the MaterialComponent, then your theme must extend from the following theme - Theme.MaterialComponents.Light.DarkActionBar
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
If you want to keep using your old styles but only want to extend from 'Theme.MaterialComponents' then you can use 'Bridge'.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorAccent</item>
<item name="colorAccent">#color/colorPrimaryDark</item>
</style>
if your theme does not extend MaterialComponents theme, and you want to keep the AppCompat theme, use a bridge theme, that would allow you to use material components keeping the AppCompat theme.
change your existing theme from:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
to this:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Does my theme extend from Theme.MaterialComponents? Oh yes indeed, and has been since I started using any of the new Material UI stuff.
If all these answers here are as unhelpful to you as they were to me, get ready: The This component requires that you specify a valid android:textAppearance attribute error can be related to an external library specifying android:theme with the same name as the theme you are using, and Android randomly deciding which one to use depending on your build.gradle. In my case the culprit was inside Mobile FFmpeg.
I started encountering this error after working for a week while the build variant was set to a different product flavor and then switching back the original one. What changed meanwhile? After thorough investigation I found the build broke after I split implementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS' in two, for each product flavor where I actually use it like this:
videoImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
mainImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
This was enough to trigger This component requires that you specify a valid android:textAppearance attribute for flavor main, while working fine for flavor video. Every main build crashed because my app's theme is named AppTheme and the Mobile FFmpeg manifest also specifies android:theme="#style/AppTheme" (which affects all versions up to 4.2.2). So I renamed my theme, but that resulted in a build error very similar to the one here:
https://github.com/tanersener/mobile-ffmpeg/issues/206
Attribute application#theme value=(#style/ScryptedTheme) from AndroidManifest.xml:37:9-45
is also present at [com.arthenica:mobile-ffmpeg-https:4.2.LTS] AndroidManifest.xml:17:9-40 value=(#style/AppTheme).
Suggestion: add 'tools:replace="android:theme"' to <application> element at AndroidManifest.xml:31:5-95:19 to override.
After adding said tools:replace="android:theme", everything worked again, and the original MaterialComponents error was gone.
But why is it OK for one flavor and not OK for the other? Absolutely no idea. Credit goes to Google's tendency to add the craziest bugs to "stable" releases. At least it's possible to solve very easily with some refactoring.
TL;DR
THIS is the issue: https://github.com/tanersener/mobile-ffmpeg/issues/206
Together with the fact that when two merged manifest specify different themes with the same name, Android will choose one randomly depending on the content of your build.gradle.
Solution: Add a tools:replace="android:theme" to your manifest's <application> tag and change the name of your theme.
Check if your AppTheme inherits from MaterialComponents as specified here.
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
</style>
Remember to check all variants of styles.xml file. This was actually the issue I had.
I had the same problem, I changed my activity theme but it didnt resolved the issue. The i changed my main app theme from AppCompact to Theme.MaterialComponents
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme2">
<activity
android:name=".MainActivity"
android:label="#string/app_name"/>
</application>
Got stucked in this error even if my theme extends Theme.MaterialComponents. I was creating Chips like this :Chip chip = new Chip(getActivity().getBasecontext(), null, R.attr.CustomChipChoice);.
The solution is to change it to Chip chip = new Chip(getActivity(), null, R.attr.CustomChipChoice);.
Hope it helps.
I have included this dependence first
implementation 'com.google.android.material:material:1.2.0-alpha01'
Than I have set my project style as bellow
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Extending Correctly? => Verify your styles!
In my case, I made a dumb auto-complete mistake:
<item name="textInputStyle">#style/Widget.MaterialComponents.Button.OutlinedButton</item>
What I really meant was TextInputLayout.OutlinedBox instead of Button.OutlinedButton:
<item name="textInputStyle">#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>
Unfortunately this typo gives the same misleading error (despite extending the theme correctly):
Caused by: java.lang.IllegalArgumentException: This component requires
that you specify a valid TextAppearance attribute. Update your app
theme to inherit from Theme.MaterialComponents (or a descendant).
...sharing because it took me a long time to spot the error!
If you tried other answers on how to properly set parent theme for your app's theme, and you still get ...requires a value for to be set in your app theme., you may check if you have more than one place, where your app's theme is defined, as you may have separate themes.xml files for specific configuration, API versions etc.
For example:
res/values/themes.xml - default one
res/values-v23/themes.xml - for devices with API >= 23

Android Failed to find style in current theme

in this simple designed Theme, Android could not find numberPickerStyle ino that and i get this error:
Failed to find 'numberPickerStyle' style in current theme
My theme is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Tsms" parent="#style/Theme.AppCompat.Light">
<item name="numberPickerStyle">#style/SampleTheme.Tsms</item>
</style>
<style name="SampleTheme.Tsms" parent="#style/Theme.AppCompat">
<item name="numberPickerStyle">#style/NPWidget.Holo.Light.NumberPicker</item>
</style>
</resources>
how to fix and resolve this problem?
I am assuming you copied your code from this StackOverflow answer. numberPickerStyle would be an attribute that is defined in your app. The code that you copied from the answer does not work. You would need to define an attribute in XML for the error to go away but that still would do nothing for you.
You can't style a NumberPicker. android:numberPickerStyle is private (see this issue).
Unfortunately, you can't style it. The styles and styling attributes
for NumberPicker are not present in the public API, therefore you
can't set them and change the default look. You can only select
between light and dark theme. source

No Resource found that matches the given name Xamarin Android

I always get the error "No Resource found that matches the given name" in my themes.xml file no matter which resource I use or in which I use it. Even if the resource works everywhere else.
Here's some code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="Theme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
<item name="android:actionOverflowButtonStyle">#style/ActionBarOverflowStyle</item>
</style>
<!-- ActionBar styles -->
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#008A3D</item>
</style>
<style name="ActionBarOverflowStyle" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">#drawable/OverflowIcon</item>
</style>
<!--Dialog styles-->
<style name="DialogStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#008A3D</item>
</style>
</resources>
I deleted the item node to check whether I can use the resource in code.
Proof that it worked; No error:
After googling and fiddling around for a few more hours I figured out that the problem was due to the fact that I use uppercase characters in the file name which works fine everywhere else. This is either a terrible behavior of Android or a bug in Xamarin.
For anyone else that finds this question for different reasons:
Whenever I Copy or Rename a drawable resource in VS 2017, it changes the 'Build Action'.
Double check your resources, and ensure the Build Action is set correctly. In my case, it needed to be 'AndroidResource'
Remove the image from the project, clean the project, add the properly renamed image(don't try to rename the image once you have added to the project). Should work perfectly.

Abstract out a drawable for different OS versions for a theme?

I'm targeting api 14 and above. In /values/styles.xml, I have this:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowBackground">#drawable/foo</item>
</style>
but for api 16 and above, I'd like to use a different drawable for windowBackground.
The "AppTheme" entry has a lot of attributes set in it right now, and I don't want to duplicate AppTheme in a v16 folder. Is there a way to abstract out just the windowBackground attribute, something like:
// /values/styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowBackground">#myDefinition</item>
</style>
<drawable id="myDefinition">#drawable/foo1</drawable>
// /values-v14/styles.xml
<drawable id="myDefinition">#drawable/foo2</drawable>
This way I can keep AppTheme defined in only one place?
Thanks
You can create a drawable folder for v-16 or v-14 so when the device is api 16/14 then it will grab that folder and use your drawable to your Style xml .
drawable-xhdpi-v16
It would seem that using drawable-v14/ would be a better solution.
See this other StackOverflow question. It's not a duplicate of your question, but it seems to give the right answer.

Android set textcolor of actionbar, example not working

This has been posted before but I still cannot figure it out. I want to change the text color of my actionbar, preferrably programmatically (is that even possible?).
I looked at examples at:
https://developer.android.com/training/basics/actionbar/styling.html
but I get "or: Error retrieving parent for item: No resource found that matches the given name '#style/Theme.Holo'."
I am using For Android 3.0 and higher only:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
</style>
...
Isn't there a simpler way to do it? I mean you can get the actionbar but there seems to be no method .setTextColor, why not?
Instead of
parent="#style/Theme.Holo"
use
parent="#android:style/Theme.Holo
Is there a Theme.Holo in your style directory? Else you might want to use #android:style/Theme.Holo

Categories

Resources