10

I'm building an Android application and right now I want to change the status bar (the one on top of the screen that show wireless and area signals, battery status, etc) but it is not working.

On the preview of the XML file it shows a different color (the shade of orange I want to use) for the bar but when I run the application both in the emulator and on my cell phone the color stays white.

I hide the action bar and put a grey image on the top of the app.

This is the preview in Android Studio for the XML file

This is the preview in Android Studio for the XML file

And this is the emulation of the application:

Emulation of the app

This is my colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="theme_color">#D8540D</color>
    <color name="toolbar_title_color">#E66800</color>
    <color name="grey">#d3d3d3</color>
    <color name="maroon">#7f0000</color>
    <color name="color_textual">#8D5020</color>
    <color name="chef_bg_color">#F7F7F7</color>
    <color name="line_color">#dddddd</color>

</resources>

And this is my styles.xml:

<resources>

    <!-- 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="android:statusBarColor">@color/theme_color</item>
        <item name="android:navigationBarColor">@color/theme_color</item>
        <item name="android:colorEdgeEffect">@color/theme_color</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mvaguimaraes.bt">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainScreen"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>

        <activity android:name=".OrderDetails"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>

        <activity android:name=".PastOrderDetails"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>
    </application>

</manifest>
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50

5 Answers5

14

In you manifest file for all the activities you specified theme as NoActionBar.

But in styles.xml inside NoActionBar theme you haven't added below line of code:

<item name="android:statusBarColor">@color/theme_color</item>

Add above line of code in style of NoActionBar theme then color of your status bar will change for sure.

But yes it will work for api level 21 and above.

Gaurav Singh
  • 2,200
  • 16
  • 30
  • `NoActionBar` theme derives attributes from `AppTheme` using dot notation, so `android:statusBarColor` is set and there's no need to add that line. – Zielony Dec 17 '17 at 00:26
4

You can change your status bar color using these few lines:

Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
window.setStatusBarColor(Color.parseColor("#your color"));
Pang
  • 9,564
  • 146
  • 81
  • 122
Vajani Kishan
  • 293
  • 4
  • 13
2

In KitKat status bar colour will not changed, but you can see it changes in Lollipop and Marshmallow.

In "res/values/colours"

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

here after kitkat, "colorPrimaryDark" will be applied for status bar and "colourPrimary" will be used for toolbar/actionbar automatically.

may be this link helps https://developer.android.com/training/material/theme.html

P Yellappa
  • 436
  • 5
  • 11
1

Place the code given below in the .java file of the activity to change the status bar color:

Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#0D1528"));
Dave
  • 3,073
  • 7
  • 20
  • 33
Akash Jaiswal
  • 31
  • 1
  • 2
0

If you are using DrawerLayout you can use below method

drawerLayout.setStatusBarBackground(R.color.status)
Ahmet B.
  • 1,290
  • 10
  • 20