1

I am following this tutorial to use colors in (my ActionBar and StatusBar) in Android Material design. I did follow the tutorial but no color is reflected, and the same dark ActionBar and StatusBar are shown when I run the app in my API22 emulator, while color is shown in the ActionBar of Pre-lollipop devices The question is why and how can I fix this?

minimumSdkVersion is 8 and targetSdkVersion is 22. It compiles with 5.1.1 API 22. The emulator to test for Lollipop devices is Nexus One but customised to run API 22, whereas the emulator to test for Pre-lollipop devices is running API 08

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark)); 
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primaryColor">#FF5722</color>
    <color name="primaryColorDark">#E64A19</color>
    <color name="accentColor">#9C27B0</color>
</resources>

res/values/styles.xml

<resources>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar" ></style>


    <style name="AppTheme" parent="AppTheme.Base">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/accentColor</item>

    </style>

</resources>

The same (as ^) code is there in res/values-v11/styles.xml and res/values-14/styles.xml.

res/values-v21/styles.xml

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

    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:colorPrimary">@color/primaryColor</item>
        <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:colorAccent">@color/accentColor</item>
    </style>

</resources>

The same (as ^) code is there in res/values-v22/styles.xml.


EDIT The left one is running API 22, and the right one is running API 08.

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Solace
  • 8,612
  • 22
  • 95
  • 183

2 Answers2

1

in MainActivity.java change

public class MainActivity extends ActionBarActivity {

to

public class MainActivity extends AppCompatActivity {

in res/values/styles.xml

replace everything with this

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#e23c7f</item>
    <item name="colorPrimaryDark">#dae22b</item>
    <item name="colorAccent">#5863e3</item>
</style>

If none of the above works, add the following snippet inside the onCreate() method

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources() 
.getColor(R.color.primaryColorDark)));

Finally you will get something like this:

enter image description here

georgeok
  • 5,321
  • 2
  • 39
  • 61
  • of course. Did you set the theme in the manifest? android:theme="@style/AppTheme" – georgeok Aug 04 '15 at 11:03
  • There is no other way this happening. Does the Android studio preview the right color or the wrong? – georgeok Aug 04 '15 at 11:18
  • I just added a screenshot to the question. It is showing the color in the ActionBar in a Pre-Lollipop device, but not in a Lollipop device :s – Solace Aug 04 '15 at 11:27
  • did you also changed the res/values-v21/styles.xml content with the style in my answer? – georgeok Aug 04 '15 at 11:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85106/discussion-between-solace-and-giorgos-oikonomou). – Solace Aug 04 '15 at 12:02
0

It seems you are using api 22; in styles 21.xml change android:colorPrimary to colorPrimary just like in styles.xml for older devices, and all rest color values in styles from v21.xml

Adriaan
  • 17,741
  • 7
  • 42
  • 75