TheHebrewHammer
TheHebrewHammer

Reputation: 3138

Draw image under Android navigation bar

I'm trying to create a layout in which my root view takes up the full screen and is drawn under the status/navigation bars. Everything works well until I have a tall popup window (the one in the image has 60 items). The window extends far below the window and cannot scroll. How can I get my layout to extend under the navigation bar without allowing popup windows to extend past the screen?

The culprit is

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

The issue is that without that flag, I can't seem to draw my view under the navigation bar. Instead I can only set the android:windowBackground (seen as green).

Here's the layout I'm using:

<android.support.constraint.ConstraintLayout
    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="@color/colorAccent"
    android:fitsSystemWindows="false"
    tools:context="com.example.fullscreendemo.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.v7.widget.AppCompatSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/spinner_values"
        android:clipChildren="true"
        app:layout_constraintTop_toBottomOf="@+id/text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <View
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="@drawable/bg_nav_bar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

The main activity:

class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecoreView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setContentView(R.layout.activity_main);
    }
}

No limits set With limits

Upvotes: 3

Views: 1460

Answers (1)

TheHebrewHammer
TheHebrewHammer

Reputation: 3138

OK, so clearly right after posting this I figure out what's going on. The documentation for Window.setFlags says that FLAG_LAYOUT_IN_SCREEN and FLAG_LAYOUT_INSET_DECOR will be used when calling setContentView or getDecoreView. It was the insets that were causing the bottom of my activity to "lift". I was able to avoid all of this by simply removing most of the code in my activity and only leaving in

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);

EDIT The above isn't enough when using a device without a soft navigation bar, to accommodate all devices use:

private void setFullScreenFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    if (!hasSoftNavigationBar()) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    AndroidBug5497Workaround.assistActivity(this);
}

private boolean hasSoftNavigationBar() {
    Display display = getWindowManager().getDefaultDisplay();
    Point realSize = new Point();
    Point displaySize = new Point();

    display.getRealSize(realSize);
    display.getSize(displaySize);
    return !realSize.equals(displaySize);
}

Upvotes: 2

Related Questions