androidandroid-optionsmenuandroid-window

Options sub-menu goes off-screen when enabling FLAG_LAYOUT_NO_LIMITS


I am setting WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS flag in order to have the status bar overlapped with the ActionBar and I have an options menu with a sub-menu.. the problem appears when I hit the sub-menu item, then it shows off the screen (its left edge can be barely seen).

When I remove the below flag, the sub-menu shows normally.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
                           WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Here's the menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:title="Items"
        app:showAsAction="never">
        <menu>
            <item
                android:id="@+id/item2"
                android:title="item 2"
                app:showAsAction="never" />
            <item
                android:id="@+id/item3"
                android:title="item 3"
                app:showAsAction="never" />

        </menu>
    </item>
    
    <item
        android:id="@+id/item1"
        android:title="Item 1"
        app:showAsAction="never" />

</menu>

And the activity if it helps

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
                                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

How to solve this issue while keeping the status bar overlapped with the ActionBar?


Solution

  • Try this:

    set the second parameter of setFlags to:

    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
    

    instead of

    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    

    or simply replace your code with this:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);