user3661367
user3661367

Reputation: 119

My navigation Drawer not close after I click the menu item?

I am the beginner to android. I want to create a navigation menu drawer. When I click the icon it will open. And it display the fragment when I click the menu item. But my Navigation drawer doesn't close automatically when I click the menu item. Everytime I closed the navigation drawer to see my fragment. I don't know where to change my coding? Herewith I attached my MainActivity.java, activity_main.xml. Anyone please help me.

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_layout"
    tools:context="com.example.yuvi.navdrawermanual.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/main_msg"
            android:textSize="20dp"
            android:textAlignment="center"
            android:text="@string/main_layout"/>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frame_container">
        </FrameLayout>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/main_drawer"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private NavigationView navigationView;
    private android.app.FragmentTransaction fragmentTransaction;

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

        mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        navigationView=(NavigationView) findViewById(R.id.main_drawer);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
                android.app.FragmentManager fragmentManager= getFragmentManager();
                if(id==R.id.nav_acc){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new FirstFragment()).commit();
                }
                else if(id==R.id.nav_set){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new SecondFragment()).commit();
                }
                else if(id==R.id.nav_logout){
                    fragmentManager.beginTransaction().replace(R.id.frame_container,new ThirdFragment()).commit();
                }
                return false;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    } 
}

Upvotes: 2

Views: 4658

Answers (2)

Kishan Solanki
Kishan Solanki

Reputation: 14618

Either you can use

drawerLayout.closeDrawer(GravityCompat.START)

or

drawerLayout.closeDrawers()

Upvotes: 0

Divyesh Patel
Divyesh Patel

Reputation: 2576

put this line:

mDrawerLayout.closeDrawer(GravityCompat.START);

after:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
     /*-------------------HERE--------------------*/

Upvotes: 7

Related Questions