JasonM JasonM - 13 days ago 6
Android Question

Set statusbar -> black color

I'm trying to set my app theme without actionbar (title) and with black statusbar, but I can't accomplish that. I went through old topics about this, but didn't find any solution. Any help?

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

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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



Answer

I would recommend setting the color in your custom theme. Starting in lollipop (and with AppCompat), the values correspond as shown in the image below:

Theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimaryDark">@color/primary_dark</item>
</style>

Then in your manifest, set the theme to AppTheme.

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

If you want to do it programmatically, try:

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