I have one Activity application. It displays a LoginFragment , which makes the transparent status and navigation bars transparent so that it can display the background image behind it. However, after entering the system, this fragment is replaced by another fragment, which should display the usual solid status and navigation lines. Thus, before deleting LoginFragment it disables the set flags to make the bars opaque.
The problem that I encountered is that after entering the system, the fragment with the usual statuses and navigation panels has the action bar shifted down. If I turn my screen to the landscape and then return to the portrait to slow down the restoration of the layout, the action bar will return to the correct position.
When the status bar is translucent. It's fine. 
On the next screen, the action bar is shifted down. Also note that the status bar is black in color, it should be gray, according to the topic. 
Now, if I go back to the first screen, the top translucent panel is fine, but all the content is pushed up, leaving an empty space below. 
Code in LoginFragment , which programmatically makes the strips translucent, and also restores them: -
@Override public void onResume() { super.onResume(); if (hasTransparentStatusBar()) { setStatusBarTranslucent(); } } @Override public void onPause() { if (hasTransparentStatusBar()) { setStatusBarOpaque(); } super.onPause(); } protected boolean hasTransparentStatusBar() { return true; } protected void setStatusBarTranslucent() { if (Build.VERSION.SDK_INT >= 21) { Window window = getActivity().getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } protected void setStatusBarOpaque() { if (Build.VERSION.SDK_INT >= 21) { Window window = getActivity().getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }
Layout xml activities: -
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.app.MainActivity" > <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@color/divider" android:dividerHeight="0dp" android:background="@color/primary" /> </android.support.v4.widget.DrawerLayout>
LoginFragment xml layout: -
<ImageView android:layout_width="wrap_content" android:layout_height="200dp" android:src="@drawable/logo" android:id="@+id/logo" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <Button android:padding="@dimen/default_margin" android:text="Login using Facebook" android:textColor="@android:color/white" android:background="@drawable/com_facebook_button_background" android:id="@+id/authButton" android:layout_below="@+id/logo" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
android android-layout android-fragments
Applegrew
source share