Why put color and dimen resources to the xml file in Android is not always a good idea?

Everything below is just thoughts. If you also have some ideas about this topic please leave a comment and I will be happy to discuss it.

Let’s start from the words about ‘put your color and dimen resources to the respective files because this is a good practice’. I think every Android developer hear or read this at least one time and I totally agree with these words. But development utopia doesn’t exist. Rapidly changed specification and businesses needs always take some places.

Of course if we have well built communication with designers and rest of the team we can do something like creation of color palette, fixed sizes across application and specify a lot of other things. There are a lot of good practices about that.

But let’s be realistic. Support projects, startups with a lot of ideas. Everything may change next day.

Okay, about resources…

In case you put all the colors from the app, even separated by screen, it will be total mess if there are no palette. As a result your color.xml file may look like this(optimistic variant):

<color name="welcome_button">#d27e21</color>    
<color name="welcome_button_pressed">#b22331</color>
<color name="main_button">#d65320</color>
<color name="main_button_pressed">#ac4000</color>
<color name="bang_button">#fabbfa</color>
<color name="bang_button_pressed">#d911d9</color>
<color name="bang_button_border">#c7a4c7</color>
<color name="search_field">#ff00ff</color>
<color name="search_field_border">#cf0fcf</color>
<color name="auto_complete_image">#fafafa</color>

What if simply put theses colors to selector? Anyway they will be there…

Of course you could try to generify it and make some order. But as soon as something will change it will be very hard to make it right. So my idea here to leave all the colors related to specific component in the layout file, selector or shape.

In case you have color.xml in bad condition it will not break anything in future. After that your color.xml file will have only colors related to the more than one component with respective name which not specify anything. For example: ….app_blue.

Actually the same story for dimen resources. Sure, there are more edge cases caused by different screen sizes but anyway it will not help if the dimen value is a small space or size which exist only in one place. In case of optimization for different screens it will not take a lot of time to create new value across your dimen.xml files. Anyway try to be as general as possible to have possibility to reuse this resources in any place and don’t stick to specific screen.

Also, since the last time I`ve use ConstraintLayout, I was not able to set values from dimen.xml as they were overwrite after each minor change. Maybe someone know the solution to fix it? Or this is a bug? Or maybe Google have some ideas how to create layouts in future?

Anyway, always exist exclusions and it’s developer choices what to do if there are no convention in the team.

To solve that issue, you can try, at the beginning of the project(with existing one, I don’t think that’s possible), to talk with design team and iOS, Web guys about name convention of the resources so everyone will use the names for colors and sizes. Or make something good for your project by iterations.

Hope you get my point. Have a nice day👍

Good reference with example: