简体   繁体   中英

Navigation Drawer not opening when I clik on Hamburger button in mainActivity

Hello I am trying to implement Navigation Drawer Menu in my MainActivity , it shows hamburger button, but when I click on it, the menu is not opening. Following are my codes:

activity_main.xml:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context="com.example.user.bottomsidenavigation.MainActivity">

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    app:headerLayout="@layout/header"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:background="#fff"
    app:itemTextColor="#000"
    app:itemIconTint="#000"
    app:menu="@menu/main_menu"
    android:layout_gravity="start">


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

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

MainActivity.java

public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle mToggle;
NavigationView navigationView;

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

    drawerLayout=(DrawerLayout) findViewById(R.id.drawer);
    mToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView=(NavigationView)findViewById(R.id.navigation_view);
}
}

Styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Anyone please help me. Thanks in advance...

Try this

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

You are setting home button enable. set it disable.

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

then initialize drawer

 private void initNavigationDrawer(Toolbar toolbar) {
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM