How to change default color at one XML/Activity only

Based on my question, how to change from blue color (Marked) to white color without hiding the battery status and wifi. Below is the current XML looks like

Main

Anyone can help?

3 answers

  • answered 2019-05-22 03:27 ismail alaoui

    If you want to change the status bar color programmatically (and provided the device has Android 5.0 or higher ) then you can use Window.setStatusBarColor()

    Just try doing:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.BLUE);
    }
    

  • answered 2019-05-22 03:52 Sultan Mahmud

    You can do it programmatically or XML color update according to yours choice.

    Using XML:

    You can create a new theme in res/values/styles.xml where you will change the "colorPrimaryDark" which is the color of the status bar:

    <style name="AppTheme.BlueStatusBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimaryDark">@color/colorBlue</item>
    </style>
    

    And modify the activity theme in AndroidManifest.xml to the one you want, on the next activity you can change the color back to the original one by selecting the original theme:

    <activity
        android:name=".XActivity"
        android:theme="@style/AppTheme.BlueStatusBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    This is how your res/values/colors.xml should look like:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#c6d6f0</color>
        <color name="colorBlue">#757575</color>
    </resources>
    

    Programmatically:

    In Kotlin: Just one line.

    window.statusBarColor = ContextCompat.getColor(this, R.color.colorBlue)
    

    In Java:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorBlue, this.getTheme()));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorBlue));
    }
    

  • answered 2019-05-22 04:28 Dhaval Solanki

    Here is the code snippet throw which you can change your status bar color and object or text color from the status bar

    add bellow code your values-v21/styles.xml, This is will work from lollipop (5.0)

    <resources>
        <!-- Theam for activity or application where ever you want to change color of your status bar -->
        <style name="AppTheme" parent="Theme.AppCompat.Light">
            <item name="colorPrimary">@color/color_primary</item>
            <item name="colorPrimaryDark">@color/color_secondary</item>
            <item name="colorAccent">@color/color_accent</item>
    
            <item name="android:statusBarColor">@color/colorPrimaryDark</item>
        <item name="android:windowLightStatusBar">true</item> 
        </style>
    </resources>
    

    statusBarColor change the color of the status bar and windowLightStatusBar will change the color of text or object from a status bar like if you set windowLightStatusBar as true then font and object will be white or light same as if you set false then it will be dark