How to automatically change drawables depending on a flavor? - android

I'm working on a really messy app and first time with flavors. App has like 4,5 flavors all with identical background colors, drawables, etc. Only logo is different.
Now I have to make a new flavor with completely different layout. Background color should be yellow instead of grey and drawable images should be black instead of white. The problem is, I have like 100 different white images. Is there a way I can change them to black automatically? (designer can redraw them or whatever to be black and I can put them in drawable folder of that flavor) Or I would have to programmatically check if new flavor is used then call setDrawable(newBlackImage) on every widget that uses white drawable? That seems like a very complicated way to do since all drawables are set via XML layout..
The way they've been using flavors was creating resource bool file and then check in code if some key has value true/false then hide/show some things... So I assume I should do something similar with this, or no?
This is what the project structure for one of my flavors looks like:
As you can see, every flavor only has different launcher and login screen icons. Also each has their own arrays, bools and strings resource files. But all of them share same layout resources and drawables with white icons.

I think you're already in the right way. By creating specific flavours for each group of drawable in your gradle file you'll just need to create a drawable folder for the flavours with specific files.
For example, the flavour 'black' will need a drawable folder in which all your black-background images will be stored.
In order to get it working, all resources must exist for all flavours so you won't need to change any part of your code.
You can find a quick guide here:
https://medium.com/#thiagolopessilva/the-handling-multiple-java-source-and-resources-using-flavors-on-gradle-18a4b581285b

Related

Three flavor dimensions, want to cover two

My Android app has three flavor dimensions. Let's call them Color, Size, and Shape. So I have (Blue|Red)(Small|Large)(Round|Square).
Is it possible to have a source folder for blueSmall that covers both blueSmallRound and blueSmallSquare? If I go straight ahead and create blueSmall under src, Android Studio doesn't see it.
I want to place a manifest there, not Java sources, so having a library with just Color and Size as dimensions is not an option.

Android: 9-patch image vs Shape vs Multi icon

Some people from our company create some rule about UI design for Web, Android and iOS. They give us a bunch of icon for each platform. But when i look at Android platform icon, they using multi icon to create 1 icon. For example, button background, they use these 3 icon, 1 for left side, 1 for right size and 1 stretchable for middle.
I think this is how web normally design button background. But i don't think i should do the same for Android (Maybe iOS also don't do it this way but since i only work with iOS for a short time, i not really sure). Normally i would use a Shape or 9-patch drawable for this kind of background. So here i have 2 question:
Which 1 should i use for button background in this example. A shape? A 9-patch drawable? Or multiple icon like this? And which one is worst to use?
If i must have to use multiple icon like this, how to put multiple icon like them into single background?
If their way of doing thing here is incorrect, i need some good reason to talk to them and make them change their mind. So any1 please help me clear about this problem.
Thank you!
9-patch should be the first choice and then is shape. Don't use 3 icon.
9-patch is more easy to use and easy to change if the further design changes. When using shape, you need to define the corresponding XML. If in next version, the background color changes, you need to modify the XML, but with 9-patch you just need to replace the resource file.
The other benefit is that, 9-patch can be created in UI side, programmer's don't have to re-manipulate it if the resource is correctly created. But with shape, programmer needs to know the RGB color, and need to code the XML.
3 icon is not suitable here. First, you have other better options. Second, 3 icon means you need to create 3 views for each of them. Third, the button should have different effect when user click it, with other two options you can simply define selectors, but with 3 icon, there's no elegant solution.

Different states of one image

I am making an app for Andorid. There are 2 restrictions:
It must be light (2-3MB)
I need to have minimal graphic files (not having a file in diffrent state and option)
How to make the layout so I can modify only the red dots (image or code) for each option that is selected?
Making 6 different states of this image is not a good answer (too big data)
Make this image as two layered. One for your main button, and other for red dots(They should be PNG's and 6 of them for different dots).And place these PNG above your main button according to the choice(Make them visible/invisible). Do you have its PSD file btw?

Select layout xml files most appropriate, depending on screen size

I would like to know if You Could select a layaout xml Differently Depending on the size of the phone screen.
I try to be more clear, I make more layout xml file, and I would like my app handles this statement
setContentView (R.layout.main)
first verifying on that size of the display is performed, and Therefore You Set the most appropriate layout xml
What you could actually do is the following:
Create a new folder for each desired configuration and name that folder with qualifiers.
If you want your application to have a different main.xml layout for different screen sizes you should make the following folders in your project.
/res/layout-small
/res/layout-normal
/res/layout-large
/res/layout-xlarge
In each of those folders have your different main.xml file layout (or whatever resource you want). Android will pick the one it matches with the device is running your application.
You can even add different resources for different configurations for drawables and even for language.
More info: http://developer.android.com/guide/topics/resources/providing-resources.html
Have you looked at this page yet?
You can query the size of the device screen, and then select which xml layout you want to use for that size screen...

are color and dimension resources necessar?

In Android is it necessary to define the colors and dimensions as xml resources in res directory as opposed to just directly specifying the color code/dimension in the layout xml files.I understand with strings you define them in resources for localization.What about for these 2?
Actually it is not necessary, but it may help you in many situations, ie. if you want to change the textcolor of all your textviews, you only need to change it in your "color"-file.
is no necessary to have them in xml, but it's more useful like that, you write less code if you want different colors for different screen sizes and it's a standard... there are a lot of benefits of using xml...

Categories

Resources