0

I did some changes to my project, like adding android.support.v7.app.ActionBarDrawerToggle / ActionBar / ActionBarActivity / Toolbar. Everything works well, the only issue I have is that I don't know how to change the ActionBar background color, it's grey and the text is black. This is what I tried:

themes.xml

<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme1" parent="@style/Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>

        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>   
     </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/blue</item>
        <item name="background">@color/blue</item>
    </style>

</resources>

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/zenyt"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme1">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

and in strings.xml I added this line:

<color name="blue">#FF2E4976</color>

I also tried to programatically change the background color:

ActionBar bar = getSupportActionBar(); 
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF2E4976")));

But I get this error:

 Caused by: java.lang.NullPointerException
            at com.zenyt.MainActivity.onCreate(MainActivity.java:67)

Line 67 is this one: bar.setBackgroundDrawable(newColorDrawable(Color.parseColor("#FF2E4976")));

Another way would be this:

<item name=”colorPrimary”>@color/my_awesome_red</item> 
<item name=”colorPrimaryDark”>@color/my_awesome_darker_red</item>

In order to use colorPrimary I need to change the minSdk to 21, but I'm testing the app on a Galaxy S3 ( 4.1.2 ). My current minSdk is 16 and I want to keep it like this. So, can someone help me?

EDIT I decided to also add the Toolbar, maybe I'm missing something

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

Sorry for my english.

Alec
  • 347
  • 2
  • 9
  • 29

1 Answers1

0

In strings.xml use color to define your colors

<color name="blue">#FF2E4976</color>

then

 <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
</style>
freddieptf
  • 2,134
  • 2
  • 15
  • 16