colorPrimary or primaryColor? - android

Everywhere on the web I see uses of colorPrimary until I stumbled across Google's very own Color Tool where they use:
-<resources>
<color name="primaryColor">#303f9f</color>
<color name="primaryLightColor">#666ad1</color>
<color name="primaryDarkColor">#001970</color>
<color name="secondaryColor">#fdd835</color>
<color name="secondaryLightColor">#ffff6b</color>
<color name="secondaryDarkColor">#c6a700</color>
<color name="primaryTextColor">#ffffff</color>
<color name="secondaryTextColor">#000000</color>
</resources>
Once you export your picks.
So what is the correct convention? Also, would it affect the Material Components colors of widgets?

1.If you define a color in res/values/colors.xml, then you can use any name on your own, the same when you create a variable in your Java/Kotlin class.
primaryColor
colorPrimary
primary_color
color_primary
An example from Android Developer
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
2.If you create a custom styles/themes which extends from Android styles/themes, then you must use name that defined by Android.
// colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#008577</color>
<color name="primary_dark_color">#00574B</color>
<color name="color_accent">#D81B60</color>
</resources>
// styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primary_dark_color</item>
<item name="colorAccent">#color/color_accent</item>
</style>
As you can see, in styles.xml, 3 attribute names, colorPrimary, colorPrimaryDark, colorAccent has been defined in parent theme, so you must use the same name because you want to override it.
In colors.xml, you can use any name that you think it make sense in your app.
Back to your questions:
So what is the correct convention?
There is no standard convention in this case, but from Android Developer, you just give a name for the color, no need to add color/Color as a prefix or suffix. Because we usually put all app's colors in res/values/colors.xml, and each item is inside a color tag.
Would it affect the Material Components colors of widgets?
It does not.

They are not the same, check out
Themes versus Styles

Related

Can I use a color variable defined in kotlin, in xml?

I'm trying to use a color from a variable defined like this(like the primary color from this variable)
val DefaultLightColors = lightColors(
primary = Color(0xFF2052EF),
primaryVariant = Color(0xFF0030cc),
secondary = Color(0xFFff9911))
in xml
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#color/</item>
I know they're a lot of questions regarding colors but I haven't found one addressing this.
Maybe you want to check out the colors.xml in the values dir. There you can list all the colors you need, then you can get them in the code by R.colors.** or in the style.xml with #color.
Hardcoding colors in the code is not really best practice since its hard to maintain.
A snippet of a colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="yellow">#FFFF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="red">#FF0000</color>
<color name="silver">#C0C0C0</color>
I hope this is what are you looking for

Can't change background color of Theme.Holo.Light action bar

I want to change colorPrimary of Theme.Holo.Light, but nothing changes.
<resources>
<color name="black">#FF000000</color>
<color name="whitegrey">#FFF2F2F2</color>
<color name="colorPrimary">#FF0F91CD</color>
<color name="holo_light_primary">#FF0F91CD</color>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:colorPrimary">#color/holo_light_primary</item>
</style>
</resources>
And I am using custom theme's name in AndroidManisfesto as well. What am I doing wrong?
In your manifest, you're using your custom theme, and you're ignoring AppTheme, which holds the required change. Just move this line:
<item name="android:colorPrimary">#color/holo_light_primary</item>
to your custom theme element.

Colors in main_activity.xml are not refreshed after changing them in colors.xml

I'm trying to change #color/colorPrimary to any other value but it doesn't change anything in activity_main.xml. Do I also have to change it somewhere else?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#33b5e5</color>
<color name="colorPrimaryDark">#7A1EA1</color>
<color name="colorAccent">#68EFAD</color>
<color name="white">#ffffff</color>
<color name="hintColor">#999999</color>
</resources>
Things to try.
Have one color.xml (make sure if you're a library that the calling module doesn't have its own colors.xml).
Check the theme, are you using a material theme that uses colorPrimary, et.al.?
Try a clean/build, (you can even just do cmd-F9 to MAKE the project, that should ensure the resources are built).
How are you using this "color", is the activity using a Material Theme in Android Manifest? What are you expecting to change?
Store all you colours in colors.xml file
<resources>
<!-- App theme colors-->
<color name="colorPrimary">#33b5e5</color>
<color name="colorPrimaryDark">#7A1EA1</color>
<color name="colorAccent">#68EFAD</color>
<color name="white">#ffffff</color>
<color name="hintColor">#999999</color>
</resources>
Override the application theme inside styles.xml to change the colors for you application
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="white">#color/white</item>
<item name="hintColor">#color/hintColor</item>
</style>
I can't remember what I did years ago (might be something along the lines of overriding stuff) but today I tried to change the colors but they didn't. Realized that I changed them at the wrong place. Maybe that helps.

How can we give a color with specific color code in the styles.xml in Android Studio?

I want to change the status bar color to a specific color with a color code. Only the default colors can be specified in the code as given below, is there any way to give a specific color code to get that color?
<item name="android:statusBarColor">#color/dark_blue_Shade1</item>
create a color.xml into values folder
code for color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="dark_blue_Shade1">#000080</color>
</resources>
if the color.xml already exists there then just put the
<color name="dark_blue_Shade1">#000080</color>
inside <resources> </resources> tag
create color.xml file inside values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#8E67E0</color>
<color name="ColorPrimaryDark">#59419B</color>
<color name="LightPrimaryColor">#android:color/holo_blue_bright</color>
<color name="AccentColor">#ff4081</color>
<color name="PrimaryText">#212121</color>
<color name="SecondarText">#727272</color>
</resources>
and then in style.xml file change like this
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimaryDark</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<item name="colorAccent">#color/AccentColor</item>
</style>
</resources>
From this your entire project will take this colors.
ColorPrimaryDark
is your status bar color you dont want to apply it manually system will get colorPrimaryDark as your status bar color

Changing Android App Background Including Spinners and Dialogs

I want to change default white background in my android app to another color.
I am using android:background="#HEXCODE" in my activity, it changes the background but the dialogs and spinners still have white background.
I also tried creating styles / custom theme but it changes the entire theme (the button and spinner style look like Androind 2.3 instead of ICS look and feel)
I don't want to change anything but on the background that is consistent also in spinners and dialogs.
Thanks.
EDIT
Style Code:
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:background">#FFFFBB</item>
</style>
Manifest:
android:theme="#style/MyTheme"
Try setting the color of the backgrounds of all widgets and the layout by
android:background="#color/black">
in your values folder make a xml like this
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
<color name="black">#000000</color>
</resources>
This worked for me.

Categories

Resources