I have an android application built with Xamarin Studio. I added a file named colors.xml to the Resources/values folder. The contents were:
<resources>
<color name="ViewBackgroundColor">#ffffff</color>
</resources>
To that, I was following this approach to define and use it; however, I was trying to apply it to the view's root element (found that resource elsewhere on SO, don't have exact link). So I applied it to the view by adding android:background="#color/ViewBackgroundColor" attribute to the root element. However, this generates a build error that #color/ViewBackgroundColor isn't a value. is anybody else having this issue and is there a resolution?
To reference that color, you must use all lowercase letters.
So
android:background="#color/viewbackgroundcolor"
This is because the Xamarin tools lowercases all names to be compliant with the rules Android has for resource names.
Is also important to set "Build Action" (with mouse right button on file color.xml) as AndroidResource.
Related
1.) Is there any reason to have a default value inside an android xml layout?
Ex.) The TextView below has included a default value of
android:visibility="visible"
`<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="visible"/>`
Conjecture: Because this is a default value, it has no effect, and therefore is an unnecessary line of code in the XML file. Is that line of thinking correct?
2.) If there is no reason for default values to exist in Android xml files, is there a lint plugin available to point out default value code lines in android XML files?
It is my thought that a large number of code lines in XML files are default values, serving no purpose. What can we do to reduce these lines of code?
U can create a style with your default values and use it.
For example:
<style name="DefaultTextViewStyle">
<item name="android:visibility">visible</item>
</style>
to use this:
<TextView
style="#style/DefaultTextViewStyle" />
I had some hope that the Lint inspection Redundant default value attribute assignment for xml, run via Android Studio might have done what you're asking. You can run it as specified under the Manually Run Inspections part of the Android docs. i.e.Android Studio -> Analyze -> Run Inspection by name -> enter "Redundant default value attribute assignment", then select the scope for the Lint check.
Sadly though, it doesn't pick up the case you mention above. I'm just wondering if there's something I've missed, or if this isn't intended for Android xml in some way?
I have a project in Android Studio that uses different flavors for different colors (some other things as well, but they don't matter).
What I would like is to see how a certain UI element (all defined by xml files) looks with non-default flavor?
Let's say I have to flavors, flavor A which is default and flavor B which is not. Lets say flavor A main color is red, and flavor B main color is green.
When I open layouts, they will always show as red, no matter what flavor in build variants is selected.
I'm sure there is a way to do this, but I don't know how.
Edit1: Seems I didn't explain it (thought it was implicit).
My resource files for flavors are already in different folders. Colors in resource files are of the same name. That is not the problem, the problem is that I can't see colors in layouts inside of Android Studio.
Colors are in separate folders and it works as it should. When I build and install flavors, they use their colors. But when I open a layout, it always shows with default colors in Android Studio
Let me blow out some dusts first :
You're getting default colors in your layout files is because, your layouts resides under default folder (main package in this case).
What happens here is that, when you create build of particular flavor (let's say flavorA), packaging system takes files specific to that flavors that are duplicated from default folder and replace it to provide you flavor-specific build.
So, your generated apk file contains flavor specific files (colors.xml in your case) + default files from main (res folder of main like all layouts and other resources & your class files too) that are not the part of your flavor specific folder.
That's why everything works perfectly.
Now back to point : (Hack)
What should you do to see layouts with flavor specific colors?
"Although I would not recommend this way" unless you're having different layout logic for flavor specific build, all you can do is place particular layout files (just to see rendered output) in your layout folder under your flavorA directory.
So, It'll now take colors from flavorA folder and render you layout in IDE with that colors in your layout file.
Note: Again, this is just a hacky way to see different things in flavor specific way, not a solution. Even though I don't think there's even solution for this problem because, this is not a problem & this is how multi-flavor gradle build system works and that's not a bug in this system.
Edit:
So, after some research, I find out how you can achieve such thing that 'you get rendered output based on your flavor colors'.
Let's say you're having different colors based on your flavors like below :
Now, contents of flavorB & flavorW are different for colors.xml file but having same color attributes like:
flavorB contains :
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#757575</color>
<color name="colorPrimaryDark">#212121</color>
<color name="colorAccent">#607d8b</color>
</resources>
flavorW contains :
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6d4c41</color>
<color name="colorPrimaryDark">#3e2723</color>
<color name="colorAccent">#33691e</color>
</resources>
As you can find out that number of color elements are the same but just values are different.
TL;DR
Solution : In such case, you can simply delete colors.xml from your main/res/values folder. It won't generate any error unless you're missing any flavor specific color attr for your "current build variant".
check out main dir for my project :
(As you can see here that, main resource directory doesn't contains any colors.xml file)
So, if you'll try changing your build variant and look at any layout file from main it'll opt out with that specific product flavor color for you.
Note : Beware that, If you add or remove one color from one flavor remember to also add/remove it to other flavors too or else your project might get error while building. (you can also do the same trick with other resources too like drawables, layouts, dimensions etc. just make sure every flavor contains it all with the same resource name)
Im not sure if its possible to affect layout preview with flavours, but its definetely possible with themes, so let me suggest a workaround.
1) Create two themes in your main source set:
<style name="MyStyle">
<item name="colorPrimary">#44f</item>
</style>
<style name="MyStyle.Red">
<item name="colorPrimary">#f44</item>
</style>
That is basically enough to customise layout preview: you can select theme you want.
2) To make your layouts flavor-dependent, create a theme that will inherit either MyStyle or MyStyle.Red depending on flavor
In main source set:
<style name="MyFlavorDependentStyle" parent="MyStyle"/>
In flavor source set:
<style name="MyFlavorDependentStyle" parent="MyStyle.Red"/>
and use MyFlavorDependentStyle in your layout.
if the colors are defined in XML just move the colors definition from main/colors.xml to flavorA/colors.xml and flavorB/colors.xml
I'm using my own library. In this library I defined some resources:
colors.xml (library)
<color name="colorPrimary">#000000</color>
In my app (that uses the above library). I want to override that color primary.
colors.xml (app)
<color name="colorPrimary">#ffffff</color>
I actually want to override that attribute so that the library (and my app) use the overrided one, not the declared one from library. It works fine but Android Studio keeps yelling out that:
Overriding #color/colorPrimary which is marked as private in my_lib. If deliberate, use tools:override="true", otherwise pick a different name". That makes me think that this is not a good approach.
I also try to add tools:override="true" to the resource:
<resources xmlns:tools="http://schemas.android.com/tools"
tools:override="true">
but the warning still there.
Is there any wrong with what I'm doing?. I can not pick another name because I want the library to use the overrided values.
I'm using Android Studio 2.1.1 and gradle 2.1.0 version.
Thanks.
Move the tools:override to the color tag.
There's nothing wrong with this. Android Studio is just warning you, incase it wasn't deliberate.
If you press alt-enter on the line with the warning, AS will offer to insert the override attribute for you and will put it in the correct place.
I tried to integrate HelpStack with my system but everytime I tried to add it I got compilation error about predefined colors, I'm wondering why predefined android colors are not working with this project, here is the error I got:
Error: No resource found that matches the given name (at 'android:background' with value '#color/white').
I know that this can be done using res/colors.xml, but I want to use predefined colors.
Add on for #MD's answer, If you have defined white color in your resource file, Then confirm your file location.
I mean, <color name="white">#FFFFFF</color> should be in res\values folder,
Ex, If you defined it in res\values-v21 folder, and run your app under API level 21 means, which also will cause the resource not found exception.
I hope this will help you.
I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>