Search code examples
androidandroid-layoutnavigation-drawer

Menu items do not appear in navigation drawer


I'm stuck with this thing: I'm trying to create the layout of a navigation drawer. Unfortunately I cannot make items of my menu to show up, even though the header happens to be at the right place. I'm going to provide my .xml of the layout file. Please, help me out!

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        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/menu_header"
        app:menu="@menu/new_menu">

    </android.support.design.widget.NavigationView>


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

MainActivity code

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity implements NavigationView.OnNavigationItemSelectedListener{
private String[] mMenuChoices;
private ListView mDrawerList;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

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

    mMenuChoices = getResources().getStringArray(R.array.menu_labels);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.nav_view);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
     fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    mTitle = mDrawerTitle = getTitle();
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed(){
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item){
    int id = item.getItemId();

    if(id == R.id.nav_newsfeed){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    }

    DrawerLayout drawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.settings_menu, menu);
    return true;
}


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}


}

Menu code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/group_activities"
        android:checkableBehavior="single">
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
    </group>

    <group
        android:id="@+id/group_sign_out"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_sign_out"
            android:icon="@mipmap/ic_sign_out"
            android:title="@string/label_sign_out" />
    </group>

</menu>

I just removed names of id, icons and titles. But they show up in my menu file correctly. Unfortunately my menu doesn't show up when I turn on the design widget for the preview.


Solution

  • Add last line like.

    mMenuChoices = getResources().getStringArray(R.array.menu_labels);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.nav_view);
    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_list_item, mMenuChoices));