0
\$\begingroup\$

I got my navigation bar to be the color I want of Blue background with white text.

enter image description here

Here is my code which works but this feels totally hacky. Please suggest improvements

styles.xml

<resources>
    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#6a9abd</item>
        <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
    </style>

    <style name="TitleBarTextColor" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:textColor">#ffffff</item>
    </style>
</resources>
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

You could actually name (i.e. wrap in a proper resource) the color you used there, and thus reference it easier in the future. Not only that, but properly defining resources is recommended for Android development, and depending on the tools you use, you might see them complaining if you don't.

Inside

/res/values/colors.xml

you could define yours as

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="my_amazing_blue">#6a9abd</color>
</resources>

and then reference it as

@color/my_amazing_blue

in the XML, or

Resources res = getResources();
int myAmazingBlue = res.getColor(R.color.my_amazing_blue);

in your Java code.

Also note that for Android, resource names are usually cased_like_this.

\$\endgroup\$
3
  • \$\begingroup\$ see the part I don't understand is where that Java goes. I did see many answers on Stackoverflow that had that format but I avoided them because I did not know what file to add that to and how it fit into the rest of the code. \$\endgroup\$
    – JGallardo
    Nov 4, 2014 at 0:37
  • \$\begingroup\$ Also, thank you for pointing out the colors.xml which I will now work with. But I don't see it in my project by default, I guess I can just create it in the format shown on this android example? \$\endgroup\$
    – JGallardo
    Nov 4, 2014 at 0:38
  • 1
    \$\begingroup\$ Search for something like MainActivity.java in your source files (start in the /src/ folder), it should be there by default (unless you didn't use the usual project wizards and/or customized filenames), and it drives your main activity :) Yeah, creating the colors file yourself should work w/o any issues. \$\endgroup\$
    – user29120
    Nov 4, 2014 at 0:44
-1
\$\begingroup\$

After accepting an answer that discussed resources, I have modified my code to this

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.Base.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/light_blue</item>
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
        parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>

</resources>

And then I created a file for colors

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="light_blue">#6a9abd</color>
    <color name="actionbar_text">#eff2f5</color>

</resources> 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.