Search code examples
androidlibgdxnavigation-drawerdrawerlayoutnavigationview

How to create a DrawerLayout overlay on top of the libgdx GL surface?


My GameActivity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // Activity setup
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        // Load OpenCV Camera Fragment
        camera = new CameraFr();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fr_camera, camera)
                .commit();

        // Load LibGDX
        GameFr game = new GameFr();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fr_game, game)
                .commit();
    }
    ...
}

Code of my main.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#263238"
android:id="@+id/main_screen">

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/profile_nav_bar"
    app:menu="@menu/profile_nav_bar_menu" />

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fr_camera"
        android:layout_width="match_parent"
        app:layout_heightPercent="75%" />
    <FrameLayout
        android:id="@+id/fr_game"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.percent.PercentRelativeLayout>

I would like to display navigation drawer bar( With social networks and other android features) overlay my gdx game. My LibGDX surface - is a fragment. I tried to use bringToFront, but it isn't work. How to do that?

enter image description here


Solution

  • I found the solution, but i reckon that it isn't the best variant. My code:

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                fr_game.setX(slideOffset * drawerView.getWidth());
                fr_camera.setX(slideOffset * drawerView.getWidth());
                drawer.bringChildToFront(drawerView);
                drawer.requestLayout();
            }
    };