No Resource found that matches the given name Xamarin Android - 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.

Related

?android:attr/colorPrimary vs ?attr/colorPrimary

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.

Changing themes and styles based on the Android version on build

I know I can add different XMLs for different API levels, for example having different styles for values-v21 and values-v19. What I'm trying to understand is how the build system actually works with those different values? So for example if I have the bulk of my styles common across all APIs and one item of one style changes between 21 and the rest, do I:
1) Copy the whole styles.xml into v21 and change the one value I need to change
2) Only add that one style that changed to styles.xml under v21
3) Only add that one item of that one style that changed under 21
It's confusing and I couldn't find any documentation how the built process handles merging styles.
Rules are quite clear:
While running Android selects the best-matching style
If selected style is a child style, Android merges its items with parent best-matching style
If you provide your mutable item via a reference, just define its value to match selected api version.
<style name="SomeStyle">
<item name="someColor">#color/some_color</item>
</style>
You can have some_color.xml in color-v21 folder for API 21 and a common version of this file in a color folder for all other api levels.
Example:
You want to have the following style for non-v21 API
<style name="FinalStyle">
<item name="commonText">It\'s a common text</item>
<item name="specificDrawable">#drawable/icon</item>
<item name="specificColor">#color/primary_color</item>
<item name="specificText">non-v21</item>
</style>
And the following style for v21 API
<style name="FinalStyle">
<item name="commonText">It\'s a common text</item>
<item name="specificDrawable">#drawable/icon</item>
<item name="specificColor">#color/secondary_color</item>
<item name="specificText">v21</item>
</style>
Specific-parameters differ between v21/non-v21 API, common parameters are common.
How to do it?
res/values/styles.xml
<style name="BaseStyle">
<item name="commonText">It\'s a common text</item>
<item name="specificDrawable">#drawable/icon</item>
</style>
<style name="FinalStyle" parent="BaseStyle">
<item name="specificColor">#color/primary_color</item>
<item name="specificText">non-v21</item>
</style>
res/values-v21/styles.xml
<style name="FinalStyle" parent="BaseStyle">
<item name="specificColor">#color/secondary_color</item>
<item name="specificText">v21</item>
</style>
res/drawable/icon.png
Common icon
res/drawable-v21/icon.png
v21 icon
When Android searches FinalStyle for v21, it selects FinalStyle definition from res/values-v21 as best-matching style, and merges with BaseStyle. In this example there is also another best-matching resource search, when Android searches #drawable/icon.
This is for anyone who comes across this and is still just as confused as I was, even after a lot of reading and trial & error. Hopefully this helps.
The folder structure is like #Dmitry stated.
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppBase" parent="Theme.MaterialComponents.NoActionBar">
<!-- simple: overrides colorPrimary in parent theme -->
<item name="colorPrimary">#color/brand_blue</item>
<item name="colorSecondary">#color/brand_grey</item>
<!-- sets the attributes in materialButtonStyle with style: myMaterialButton -->
<!-- the materialButtonStyle attribute is what actually changes the button settings -->
<item name="materialButtonStyle">#style/myMaterialButton</item>
</style>
<!-- this style consists of common 'attributes' among all API versions -->
<!-- you can choose to add a parent to inherit an additional style -->
<!-- unlike the materialButtonStyle attribute, this parent is not necessary to change the button settings -->
<style name="myMaterialButton" parent="Widget.MaterialComponents.Button">
<item name="cornerRadius">60dp</item>
<item name="android:paddingVertical" tools:targetApi="26">20dp</item>
</style>
<!-- this will add on and override AppBase and should include any changes that differ from other API versions -->
<style name="AppBaseChanges" parent="AppBase">
<!-- to inherit myMaterialButton, you don't have to include it in here, since it's in AppBase -->
<!-- however, if you want to extend myMaterialButton, create a new style as its child -->
<item name="materialButtonStyle">#style/myMaterialButtonAPI_All</item>
</style>
<!-- make sure the parent is myMaterialButton to inherit/override its settings -->
<!-- this will be picked for all APIs lower than other styles like this -->
<style name="myMaterialButtonAPI_All" parent="myMaterialButton">
<item name="backgroundTint">?attr/colorPrimary</item>
</style>
</resources>
res/values-v2/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- restate the same declaration as the other xml file-->
<style name="AppBaseChanges" parent="AppBase">
<!-- use a different name (...API_2) for the overriding style -->
<item name="materialButtonStyle">#style/myMaterialButtonAPI_2</item>
</style>
<style name="myMaterialButtonAPI_2" parent="myMaterialButton">
<item name="backgroundTint">?attr/colorSecondary</item>
</style>
</resources>
Set the manifest theme to AppBaseChanges. The app will pick only one AppBaseChanges style to apply changes, so be sure to carefully override the right styles to ensure you are inheriting from lower level versions.
For some reason, AndroidStudio doesn't do a good job at all previewing themes, so before you think it's not working, relaunch the app to see the changes. There are also situations where I have no idea why it wasn't updating the setting and couldn't find where it was overriding the theme. In those cases you can dig further, or avoid the hassle and just apply the relevant style directly to the view.
Here's the order of precedence for the sample themes described above. The higher the style, the higher precedence it has and will override the lower style.
either myMaterialButtonAPI_All or myMaterialButtonAPI_2
AppBaseChanges (only one is chosen)
myMaterialButton
Widget.MaterialComponents.Button
AppBase
Theme.MaterialComponents.NoActionBar
You may maintain only one styles.xml(default) file for all the device version.
Checkout my answer to How to remove repeating of similar styles in v19/styles.xml and v21/styles.xml files
https://stackoverflow.com/a/53445541/5745574

How to change color of the action bar using actionbarsherlock

I'm trying to change color of the action bar across the board in my app. I found this answer which suggests a way to do it. So I've created a themes.xml file under res/values folder with the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverFlow">
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>
<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
<item name="android:background">#fff4231e</item>
<item name="background">#fff4231e</item>
</style>
</resources>
However, I get an error in the above code:
Error retrieving parent for item: No resource found that matches the
given name 'Theme.Sherlock.ForceOverFlow'.
in my build.gradle I have the following:
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
I am using AndroidStudio and Gradle
ActionBarSherlock version 4.4 does not include the ForceOverflow themes, it was removed in 4.2. You will need to use an older version of ABS to use ForceOverflow as a theme.
As per the Change Log :
Fix: Remove .ForceOverflow themes. These never should have been
included.
If you don't actually need the ForceOverflow, you could use Theme.Sherlock, see the official page for details :
<style name="Theme.MyTheme" parent="Theme.Sherlock">
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>

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

Why can't I use #android:style/DialogWindowTitle

I needed to change title size and simple solution was to create a new style, setting parent to #android:style/DialogWindowTitle and then modifiy it. However, Eclipse keep showing an error:
no resource found
I checked in SDK styles.xml, it exists there in every version.
Why can't I override it? I want to use system default for all parameters, and just change the size.
I also got an error while trying to do this:
<style name="CenterJustifyDialogTitle" parent="#android:style/DialogWindowTitle" >
<item name="android:gravity">center|center_vertical</item>
<item name="android:textColor">#000000</item>
</style>
The error dissapeared when I changed this to:
<style name="CenterJustifyDialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle" >
<item name="android:gravity">center|center_vertical</item>
<item name="android:textColor">#000000</item>
</style>
Hope it helps to anyone interested.

Categories

Resources