Jhon Smith
Jhon Smith

Reputation: 103

Can i change the status bar icon/text color? only icon, not with background

can i change the status bar icon color? only icon.. no background. I try with setStatusBarColor with white color. Then icon like o'clock, wifi, battery not visible because it uses white. Result :

enter image description here

For example i want to change icon in status bar to black and background status bar is white, result like this..

enter image description here

could this happen in android studio?

Upvotes: 0

Views: 1040

Answers (2)

Shruti Patel
Shruti Patel

Reputation: 686

Yes, it is possible. Add this style to your theme.xml.

<style name="WhiteStatusBar" parent="Your app theme">
        <item name="android:statusBarColor" tools:targetApi="l">@color/white</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>

Go to your app AndroidManifest.xml and add above theme to your activity.

<activity
            android:name=".activity"
            android:theme="@style/WhiteStatusBar"
            android:exported="false" />

Upvotes: 1

Mehul Boghra
Mehul Boghra

Reputation: 232

Yes, You can change text and icon color using the below code

window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

Here is My fully working code, including changing background color with icon and text color.

open fun getStatusBarHeight(context: Context): Int {
    var result = 0
    val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        result = context.resources.getDimensionPixelSize(resourceId)
    }
    return result
}

open fun setWindowFullScreen(tbMain: Toolbar) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!isDarkTheme()) {
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
    }
    setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
    window.statusBarColor = Color.TRANSPARENT
    tbMain.setPadding(0, getStatusBarHeight(this), 0, 0)
}

open fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
    val window = activity.window
    val winParams = window.attributes
    if (on) {
        winParams.flags = winParams.flags or bits
    } else {
        winParams.flags = winParams.flags and bits.inv()
    }
    window.attributes = winParams
}

you just need to call setWindowFullScreen(tbMain) method from your activity with the toolbar parameter.

Upvotes: 1

Related Questions