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
Related
Hy,
I am using the next background colour #android:color/transparent for a ImageButton in the layout.xml. Could or should I externalize it in the colors.xml resources file?
Thanks
You could but it depends on your needs.
If you use that color in many different parts of your application it's best to place it in an external xml file. Sometimes you need to change the style of the whole application and it's easier to change the definition of that color instead of changing the color in many different places. If you're only using it in one single place then it is OK to leave it out of the external file.
EDIT:
<color name="my_background_color">#android:color/transparent</color>
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.
I have added spanish and french to my app but some of the wording is longer in spanish then english. how can i change the textsize when the values-es/string.xml file is accessed
You can use the dimens.xml resource file for this purpose. In your case you'll probably want to create a file called res/values-es/dimens.xml, and possibly also a -fr version. You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml, if you want to be more specific).
Example grabbed from the More Resource Types section on developer.android.com:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Apply in xml
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
Or in code
float fontSize = getResources().getDimension(R.dimen.font_size);
There are also solutions here on SO that use a iterative/recursive process to shrink the text size of a TextView to 'fit' in its bounding box (using a custom view), but I'd say above is a more robust approach, especially if you're considering adding more languages in the future.
The above explanations are correct, but they don't fully explain how to do it.
When you open your project in Android Studio, then it automatically shows this project in the "Android" mode. You need to click on the "Android" tab in the top, left corner of Android Studio and select "Project". Then you need to go into "app>src>main>res". Then you need to right-click on the "res" folder and from the menu that comes up, select "New>Android resource directory". A dialogue will come up, and for Directory name: type in values-es and click OK.
This will create a folder for all Spanish Locale values. And then you can right-click on this values-es folder to create dimens.xml, string.xml, color.xml, ...etc. files that will be used whenever Spanish Locale is selected in the phone.
If you've already created a string.xml file for Spanish Locale through the graphical user interface, then the values-es folder with string.xml file will already be in the Project, when you go there. And in this case, you just need to right-click on the values-es folder to create the dimens.xml file there for Spanish Locale.
You would need to specify a different layout file in layout-es. That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. That layout file can then specify a theme, style, or text size on the views.
I'm doing Tutorials and I'm on section about images. It says to put them into the folder res/drawable. But I don't have that folder, I have three instead: res/drawable-hdpi, res/drawable-ldpi and res/drawable-mdpi. So whats the difference between them?
Im using this tutorial.
One of the steps is:
Create a strings.xml file in
res/values/ and edit the file to look
like
There already is strings.xml, combined with the above, telling me to use res/drawable, are these tutorials out of date?
This tutorial has code like:
R.id.spinner
R.array.planets_array
R.layout is just simple enum. Uses the main.xml in the layout folder. But where are R.id and R.array to come from. Because it is coming up in eclipse saying it doesn't know what they are. R.java gets updated automatically, so can someone tell me from reading that tutorial where id gets added to R? It says that
The R.array.planets_array ID
references the string-array defined
above
Only it doesn't work. I doubt it makes a difference that i didn't make strings.xml since it's the same filename in the same location. But since R.java is meant to be updated automatically I don't know how to fix this.
Those are for the different screen resolutions for the range of devices that are out there. Read about supporting multiple screens on the Android dev site.
Just so you know where the R stuff comes from.
The R.java file is a generated file which contains some kind of pointers to a resource in your application. It is a simple integer actually which uniquely identifies the resource in the internal resource management system of Android.
R.string identifiers are generated from resources XML files like this one for example.
<resources>
<string name="test">This is a test string.</string>
</resources>
R.array identifiers from string array XML files.
<resources>
<string-array name="days_of_week">
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Sunday</item>
</string-array>
</resources>
You can access that array using its identifier R.id.days_of_week now.
R.id identifiers are a bit special.
They are generated in two ways. The first one is when you define a View in your XML layout file using the #+id/... syntax. Note the + sign.
The other way is to define them in resource XML files like strings for example.
<resources>
<item type="id" name="first" />
<item type="id" name="second" />
</resources>
You'd then just use them in a layout XML file like this #id/first. Note that there is no + sign anymore as you reference it, before you were declaring it.
In code you then use it like this, R.id.first.
There are a lot of other resources. I'd like to point you to the Application Resources article and also make sure to checkout the Resource Types sub-article.
If you don't have the folder, just create it. It is basically the fallback for the case that you don't have a resource in a more specific folder like res/drawable-hdpi
The *-xx folders allow you to provide more specific drawables (images) for various screen resolutions.
The same principle applies to values/ and values-xx/ where xx is a country code ; the xx versions allow you to have translations for UI messages.
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>