I have a single Activity application. It displays a LoginFragment
which makes Status and Navigation bars translucent so that it can display a background image behind it. However, after login this fragment is replaced by another fragment which needs to display the usual solid Status and Navigation bars. So, before LoginFragment
gets removed it unsets the flags it had set, to make the bars opaque.
The problem that I am facing is that after I login, the fragment with normal Status and Navigation bars has its Action bar displaced down. If I rotate my screen to landscape and then back to portrait to force the recreation of the layout then the Action bar snaps back to correct position.
When Status bar is translucent. This is perfectly fine.
On next screen Action bar is displaced downwards. Also notice that Status bar is black in color, it should have been gray, according to theme.
Now if I go back to first screen, the top translucent bar is fine, but the whole content is shifted upwards, leaving a blank white space below.
Code in LoginFragment
which programmatically makes the bars 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 of Activity:-
<?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" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<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
's layout xml:-
<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" />
I had the same problem before, so if anybody stumbles on this, the correct workaround is to set the NO_LIMITS flag :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With