Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make status bar opaque programmatically

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.

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.

So far I've tried the following code. But this doesn't work.

if (Build.VERSION.SDK_INT >= 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));
    }

This is the screen I'm getting:

incorrect_behaviour

But I want the status bar to have primaryDark colour and the Toolbar to appear below it.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.blogspot.smarphy.pierecipe">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".HomeyActivity"
            android:label="@string/title_activity_homey"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
        </activity>

        <activity
            android:name=".RecipesCardsActivity"
            android:label="@string/title_activity_recipes_cards"
            android:parentActivityName=".HomeyActivity"
            android:theme="@style/AppTheme.OpaqueStatus" />

        <service android:name=".Services.HomeCardsNetworkService" />
        <service android:name=".Services.SearchCardsNetworkService" />

    </application>

</manifest>
like image 344
Anand Avatar asked Dec 18 '25 06:12

Anand


1 Answers

As I understand you correctly.

You want to switch from fullscreen to normal when you switch from fragment 1 to 2.

You probably have this on your activity if not use this

android:theme="@style/FullscreenTheme"

In fragment 2 you can use this to setup non-fullscreen mode. @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ....
    }
    @Override
    public void onDestroyView() {
        getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ....
    }

You should clear fullscreen flag when adding non-fullscreen.

Info: The translucent status bar was introduced in API level 19.

like image 126
Jakub Gabčo Avatar answered Dec 20 '25 00:12

Jakub Gabčo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!