Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - defining colors by referencing other defined colors

Tags:

I've wondered time and time again if there is some way to define colors in colors.xml by referencing another color that has been defined. Well, I tried it and indeed, it appears as though you can reference other colors using @color/XXX.

For example one's colors.xml could look like:

<?xml version="1.0" encoding="utf-8"?> <resources>     …     <color name="primary_blue">#205081</color>     <color name="action_bar_text">@color/primary_blue</color>     … </resources> 

But I don't want others to use my primary_blue definition directly -- it doesn't make sense to make something blue without context. The best solution I've come up with is to prefix 'direct' colors like primary_blue and then comment that they shouldn't be used directly.

Is there any way to prevent the use of these direct colors, while still allowing my colors.xml file to reference them? I'm thinking perhaps styles or an apklib could help me out.

like image 886
Jon Willis Avatar asked Mar 27 '13 03:03

Jon Willis


1 Answers

Adding an answer here in case anyone runs across this in the future.

When creating an Android library, you can mark resources as public. Any resource that is not in the public.xml file is assumed to be private.

Implicitly making attributes private not only prevents users of your library from experiencing code completion suggestions from internal library resources but also allows you to rename or remove private resources without breaking clients of your library. Private resources are filtered out of code completion and the theme editor, and Lint warns you when you try to reference a private resource.

like image 180
AesSedai101 Avatar answered Jan 03 '23 11:01

AesSedai101