Search code examples
javaandroidandroid-layoutandroid-linearlayoutandroid-windowmanager

WindowManager addView() not respecting MATCH_PARENT


I have a problem using WindowManager to show a layout as an overlay. The layout just covers enough area to show the bare minimum area which its child views cover, rather than covering full screen.

I am using the following code to show a layout using WindowManager:

    LinearLayout linearLayout = new LinearLayout(MainActivity.this);
    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
    linearLayout.addView(inflatedLayout);

final WindowManager.LayoutParams paramsForOverlay = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, // this allows it to show over lock screen AND be able to receive touch.
                    FLAG_TRANSLUCENT_STATUS | FLAG_NOT_TOUCH_MODAL,
                    PixelFormat.TRANSLUCENT);
            paramsForOverlay.gravity = Gravity.TOP | Gravity.CENTER;


    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(new DisplayMetrics());

if (windowManager != null) {
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                windowManager.addView(linearLayout, paramsForOverlay);
            }

Here is the xml for R.layout.note_taking_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/llCanvas"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:layout_gravity="center"
        android:text="ABCD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:background="@color/colorPrimaryDark"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp">

    <TextView
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="world" />

    <Button
        android:id="@+id/bDestroy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello" />
</LinearLayout>

Here is how it should look:

enter image description here

And this is how it actually looks:

enter image description here

What can I do so that it covers the whole screen?

EDIT: I show a button and expand it to the full screen using this:

final Button button = new Button(MainActivity.this);
button.setText("tis a button");
button.setLayoutParams(
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));

Solution

  • After trial and error, I found that I was not setting MATCH_PARENT to the View inflatedLayout. So instead of this:

                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
                linearLayout.addView(inflatedLayout);
    

    I should have done this:

                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
                inflatedLayout.setLayoutParams(     // THIS!
                        new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
                linearLayout.addView(inflatedLayout);