0

I'm currently developing an android app using a Toolbar and a DrawerLayout. The main content is a custom SurfaceView where I can draw differents shapes, text ... The left menu is a NavigationView and is used as a toolbox (I select what I want to draw from the left and I draw it on the SurfaceView). Everything is working fine except for one thing : when I first try to open the left menu (by clicking the toolbar or by sliding from the left side of the screen) the items are not visible. And while I don't click on any item (which are not visible) they stay invisible. The problem is only fixed when I click on an invisible item, after that the menu is working fine. I'm using a custom theme to hide the status bar and remove the default actionbar :

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"></style>

<style name="ColladiaTheme" parent="AppBaseTheme">

    <!-- Remove action bar -->
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

    <!-- Remove status bar -->
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>

    <!-- Material theme -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

Here are some screenshots :

Menu open but not visible

Menu visible after clicking an item

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.ia04nf28.colladia.DrawColladiaView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/draw_view" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary">
        </android.support.v7.widget.Toolbar>

    </FrameLayout>

    <!-- Left navigation menu -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:menu="@menu/nav_view_items" >
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

And here is the Activity code :

public class DrawActivity extends AppCompatActivity {
private static final String TAG = "DrawActivity";

private DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
private NavigationView nav;
private DrawColladiaView colladiaView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw);

    // Change toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    colladiaView = (DrawColladiaView) findViewById(R.id.draw_view);
    colladiaView.setApplicationCtx(getApplicationContext());

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    // Add burger button
    drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(drawerToggle);
    // Removes overlay
    drawer.setScrimColor(Color.TRANSPARENT);
    drawer.closeDrawers();

    nav = (NavigationView) findViewById(R.id.nav_view);

    setUpDrawerContent(nav);
}

public void setUpDrawerContent(NavigationView nav)
{
    nav.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    selectDrawerItem(item);
                    return true;
                }
            }
    );
}

public void selectDrawerItem(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.nav_home:
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            break;

        default:
            Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());

            if (newElement != null) colladiaView.insertNewElement(newElement);

            drawer.closeDrawers();
            break;
    }
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onBackPressed() {
    Log.d(TAG, "onBackPressed");
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

Bash
  • 13
  • 5

1 Answers1

0

It looks like you should be overriding onOptionsItemSelected instead of onBackPressed:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // This will toggle the drawer if android.R.id.home is clicked
    if(drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    // Handle any other menu item selections...

    return super.onOptionsItemSelected;
}

To be honest, I do not see where your drawer is ever being opened. It should still respond to a swipe from the left edge though. This code will set it to be toggled when android.R.id.home is clicked (the back/menu button).

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Unfortunately this is not working either. I've tried with some other devices and the android emulator and it looks like it's working fine on them. I guess it is related to my phone... – Bash Jun 07 '16 at 14:33
  • @Bash Ah, are they running different versions of Android? There could be a bug in an older version. – Bryan Jun 07 '16 at 15:34
  • My phone is running in Android 5.0 (Lollipop) and the emulator in Android 4.1.1. It's working on the emulator but not on my phone – Bash Jun 07 '16 at 15:52