Luca Lotifi
Luca Lotifi

Reputation: 79

Toolbar and statusbar color doesn't match

Here's how status bar and toolbar appear:

Here's how status bar and toolbar appear

The color of toolbar must extends to the color of status bar, but I don't understand what's wrong.

Here's the activity xml file:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".view.AmministratoreActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_amministratore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:elevation="4dp"/>

</RelativeLayout>

In manifest I use android:theme="@style/AppTheme.NoActionBar for entire app

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>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    </style>


</resources>

Upvotes: 0

Views: 835

Answers (3)

Al-Amin
Al-Amin

Reputation: 1387

public abstract void setStatusBarColor (int color)

This above method Added in API level 21. So you can change status bar color programmatically using below snippet of code.

Window window = activity.getWindow();

// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));

Upvotes: 0

Brahma Datta
Brahma Datta

Reputation: 1112

Try this below the SetContentView() in onCreate() Method @Russo

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        // clear FLAG_TRANSLUCENT_STATUS flag:
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
   window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);     
   window.setStatusBarColor(getResources().getColor(R.color.your_desired_color));

    }

Upvotes: 0

Thoriya Prahalad
Thoriya Prahalad

Reputation: 2660

Put this code above setContentView() in onCreate methhod

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);
 }

Upvotes: 0

Related Questions