title: Make status bar opaque programmatically
body: <p>There are 2 Fragments of the Activity. When the first Fragment is visible, I want the status bar to be translucent (no Toolbar here). That works as expected.</p>
  
  <p>But when I switch over to the second Fragment, I want the status bar to be opaque. And the Toolbar should be drawn below the status bar. I want these changes to be done programmatically because I can't do it in the Activity XML.</p>
  
  <p>So far I've tried the following code. But this doesn't work.</p>
  
  <pre><code>if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP)
      {
          Window window = getActivity().getWindow();
          view.setFitsSystemWindows(true);
          window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_LAYOUT_FLAGS);
          window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
          window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
          window.addFlags(WindowManager.LayoutParams.TYPE_STATUS_BAR);
          window.setStatusBarColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
      }
  </code></pre>
  
  <p>This is the screen I'm getting:</p>
  
  <p><img src="https://i.stack.imgur.com/ePvFnl.png" alt="incorrect_behaviour"></p>
  
  <p>But I want the status bar to have primaryDark colour and the Toolbar to appear below it.</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="in.blogspot.smarphy.pierecipe"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"&gt;
          &lt;activity
              android:name=".SplashActivity"
              android:theme="@style/AppTheme"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=".HomeyActivity"
              android:label="@string/title_activity_homey"
              android:theme="@style/AppTheme"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.SEARCH" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=".RecipesCardsActivity"
              android:label="@string/title_activity_recipes_cards"
              android:parentActivityName=".HomeyActivity"
              android:theme="@style/AppTheme.OpaqueStatus" /&gt;
  
          &lt;service android:name=".Services.HomeCardsNetworkService" /&gt;
          &lt;service android:name=".Services.SearchCardsNetworkService" /&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>