CoordinatorLayout kids are not fullscreen

I have an Activity that displays in full screen. This works great with many of the layouts I've tried, except when CoordinatorLayout is the root of the ViewGroup . The CoordinatorLayout itself has a width and height equal to match_parent , and it takes the whole screen as it should. But the view of the child, which should be the same size as the CoordinatorLayout , fits as if the navigation bar is still visible.

Problem illustration

Is there a way to make view sizes for children using CoordinatorLayout ? Obviously, fitSystemWindows does not change anything, since it is probably caused by the implementation of CoordinatorLayout , other ViewGroups work well. I tried to create a custom Behavior class, but I failed.

I use this code to make my Activity fullscreen:

 @Override protected void onResume() { super.onResume(); int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; getWindow().getDecorView().setSystemUiVisibility(uiOptions); } 

This is a simple layout used to generate an image:

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:background="#F00"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" android:src="@drawable/background_sky_light" /> </android.support.design.widget.CoordinatorLayout> 
+5
source share
3 answers

change android: fitsSystemWindows - false if you use coodinatorlayout android:fitsSystemWindows="false "

+1
source

I had a similar problem and had to crack a little solution. Instead of ImageView, my child CoordinatorLayout is for video streaming. Since I broadcast the video during rotation, I need to watch configChanges and override onConfigurationChanged. This may not work for you if you do not want to override onConfigurationChanged, but it may work. As I said, this is a little hack, but works for me.

As a result, I completely expand the toolbar, save the offset, and then hide it (considering it as a standard view). Then, when I turn back, I resize it to what was offset when the user turned the device.

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { showToolbar(); mViewPager.setVisibility(View.VISIBLE); } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { hideToolbar(); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mViewPager.setVisibility(View.GONE); } } public void hideToolbar() { setCurrentOffset(); expandToolbarToHeight(0); findViewById(R.id.toolbar).setVisibility(View.GONE); } public void showToolbar() { expandToolbarToHeight(oldOffset); findViewById(R.id.toolbar).setVisibility(View.VISIBLE); } public void setCurrentOffset() { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); if (behavior != null) { oldOffset = behavior.getTopAndBottomOffset(); } } public void expandToolbarToHeight(int height) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); if (behavior != null) { behavior.setTopAndBottomOffset(height); behavior.onNestedPreScroll(mCoordinatorLayout, mAppBarLayout, null, 0, 1, new int[2]); } } 
0
source

You can try to remove android:fitsSystemWindows="true" from ImageView and change onResume() as follows:

 @Override protected void onCreate(Bundle savedInstanceState) { .... mRootView = (CoordinatorLayout) findViewById(R.id.coordinatorLayout); .... } public void hideBars() { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); mRootView.setFitsSystemWindows(false); } 

mRootView.setFitsSystemWindows(false); It is important to set the user's views in full screen.

Without this call, the layout will look like the status bar and navigation bar still exist. As the picture below:

Screenshot without setFitsSystemWindows (false)

And with this call:

Screenshot with setFitsSystemWindows (false)

I think the reason is that when fitsSystemWindows true, CoordinatorLayout reserve a place for the status bar and navigation bar to work with other widgets that paint the background of the bars themselves. But when we hide the bars in the system interface, there is no need to reserve space, and therefore we need to tell CoordinatorLayout to free up spaces for other child views.

Below are the layouts in the screenshot

Layout for this operation:

 <android.support.design.widget.CoordinatorLayout android:id="@+id/coordinatorLayout" 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="match_parent" android:background="@android:color/black" android:fitsSystemWindows="true" tools:context="org.hamster.carz.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/frag_container"/> <!-- Just a empty FrameLayout --> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@mipmap/car_add" /> </android.support.design.widget.CoordinatorLayout> 

Fragment in the screenshots above:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="2dp" android:layout_weight="1" android:background="@drawable/controller_button"/> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="2dp" android:layout_weight="1" android:background="@drawable/controller_button"/> </LinearLayout> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:layout_weight="1"> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_disconnect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="@dimen/fab_margin" android:src="@mipmap/ic_clear" app:backgroundTint="@color/colorAccentDark"/> </FrameLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="2dp" android:layout_weight="1" android:background="@drawable/controller_button"/> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="2dp" android:layout_weight="1" android:background="@drawable/controller_button"/> </LinearLayout> </LinearLayout> 

Hope this helps.

-1
source

All Articles