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
  • 15,941
  • 7
  • 35
  • 67
Solace
  • 7,868
  • 17
  • 74
  • 162
  • 2
    Can you post more information? Snippets of code? Anything we can look at to determine what goes wrong? – Rob Aug 04 '15 at 09:12
  • 1
    What did you try so far? Show some code. – Thomas R. Aug 04 '15 at 09:12
  • @Rob Sorry I had mistakenly posted the question earlier. Just edited it to show more information. – Solace Aug 04 '15 at 09:18
  • @ThomasR. Sorry I had mistakenly posted the question earlier. Just edited it to show more information. – Solace Aug 04 '15 at 09:18
  • 1
    where is AppTheme.Base style?? – Dhaval Parmar Aug 04 '15 at 09:20
  • 1
    @Solace you are folowing slidenerd tutorial check this may be helpful for you https://github.com/slidenerd/materialtest and your issue in style check it – Aman Jham Aug 04 '15 at 09:23
  • @DhawalSodhaParmar I am so sorry (I was a bit freaked out after posting the question too soon) I pasted the wrong file in `values/styles.xml`. Just edited and corrected in the question. – Solace Aug 04 '15 at 09:23
  • @AmanJham I never understand how to use that one app to test everything. It has tons of other features, and doesn't have a simple minimalistic app to test my app against, for the particular feature that I have used/tested in my app. =s – Solace Aug 04 '15 at 09:30
  • Is `AppTheme` set to `android:theme` in your manifest? – PPartisan Aug 04 '15 at 09:32
  • @PPartisan Yep `android:theme="@style/AppTheme"` – Solace Aug 04 '15 at 09:33
  • 1
    maybe you can try my answer about statusbar ,http://stackoverflow.com/a/30023811/998953 – user998953 Aug 04 '15 at 10:10
  • @user998953 It didn't help me =( – Solace Aug 04 '15 at 13:02

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
  • 4,233
  • 2
  • 29
  • 50
  • 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
  • 15,941
  • 7
  • 35
  • 67