11

The navigation drawer in my app is not closing. I am using activities instead of fragments. When i click on any item in the listview, it opens other activities as it should but when i go back, the drawer is still open. I have tried using DrawerLayout.closeDrawers(); but it did not work. How do I close the navigation drawer?
Here is my code:

Java

public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            switch (pos){
                case 0:
                    Intent i = new Intent(MainActivity.this,Aluminium.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent i2 = new Intent(MainActivity.this,Gold.class);
                    startActivity(i2);
                    break;
                case 2:
                    Intent i3 = new Intent(MainActivity.this,Zinc.class);
                    startActivity(i3);
                    break;
            }
        }
    });
}
}

XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView android:id="@+id/left_drawer"
    android:layout_width="220dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp"
    android:background="#000000"/>
</android.support.v4.widget.DrawerLayout>
Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63

11 Answers11

7

have you tried :

mDrawerLayout.closeDrawer(drawerListView);

You can add this before calling startActivity()

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
  • Don't know why but `closeDrawers` didn't work but `closeDrawer(Gravity...)` did, so thanks you gave me the clue I need, didn't know there were more methods – cutiko Sep 14 '20 at 19:11
5

In continuation to others answers and @ Chinmay Dabke question of 'but the drawer closes half then pauses and then closes fully' in one of the comments, here is what you could do:

first as others suggested, this line is missing. drawer.closeDrawer(navList);

And as far as the pausing of drawer is concerned while closing, you could do something like this. use a Runnable and a Handler like this:

mRunnable = = new Runnable() {
    @Override
    public void run() {
         //say
         selectItem(pos); //Implement your switch case logic in this func
    }
}

and then in the onDrawerClosed overrided method

@Override
public void onDrawerClosed(View view) {

if (mRunnable != null) {
    mHandler.post(mRunnable);
    mRunnable = null;
}
}

Hope this helps!

I would suggest you to use fragments for navigation drawer and to solve this issue of drawer not closing properly, I found this article very useful (using fragments). http://www.michenux.net/android-asynctask-in-fragment-best-pratices-725.html

droidx
  • 2,172
  • 19
  • 26
  • I put the runnable initiation in the onclick event so it could get what it needed from the row. Then as droidx did put the post in the onDrawerClosed. Fixed the stutter nicely. – George Baker Feb 04 '15 at 05:14
4

Call

drawer.closeDrawer(navList);

in onItemClick() method

ravindra.kamble
  • 1,023
  • 3
  • 11
  • 20
4

Try

drawer.closeDrawer(Gravity.START);

Your drawer gravity is start so Use that to close the corresponding drawer

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
3

I didn't see any code where you are closing the ListView from drawer... close the ListView Drawer on ListItem click...

    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
            drawer.closeDrawer(navList);
            switch (pos){
                case 0:
                    Intent i = new Intent(MainActivity.this,Aluminium.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent i2 = new Intent(MainActivity.this,Gold.class);
                    startActivity(i2);
                    break;
                case 2:
                    Intent i3 = new Intent(MainActivity.this,Zinc.class);
                    startActivity(i3);
                    break;
            }
        }
    });
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
2

You need to close the drawer on list item click

drawer.closeDrawer(navList);

Also what is the use of FrameLayout in your xml. It is not used as a container to add or replace fragments

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

call the drawer.closeDrawer(navList); function before switch case

Mohan
  • 311
  • 8
  • 15
1

use

if(drawer.isDrawerOpen(navList))  
  {  
      drawer.closeDrawer(navList);      
  }

In onResume() and start of onItemClick() method.

or you can try another approach..run a Ui thread when you are selecting drawer item

private void selectDrawerItemItem(final int position){

     //Toast.makeText(getApplicationContext(), "ItemClicked", Toast.LENGTH_SHORT).show();

     darwer.closeDrawer(navList);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Fragment fragment = new Fragment(Activity.this);
            Bundle args = new Bundle();
            args.putInt(Fragment.ARG_PLANET_NUMBER, position);
            fragment.setArguments(args);

            FragmentManager fragmentManager = getSupportFragmentManager();
             fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();

            // update selected item and title, then close the drawer
            navList.setItemChecked(position, true);

            setTitle(" " + navListTitles[position]);
        }
    }, 200);


    // update the main content by replacing fragments


}
TanvirChowdhury
  • 2,498
  • 23
  • 28
1

I was having the same problem.

I used

mDrawerLayout.closeDrawer(drawerListView);

before starting my new activity. It beautifully slides the drawer back in.

Abhishek Balani
  • 3,827
  • 2
  • 24
  • 34
1
private DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();

it works

Michael Yang
  • 1,403
  • 2
  • 18
  • 27
0

Here is the code: Lets say you have the drawer class

in your activity call the class as and make a vaiable and assign it(put the drawer layout within a fragment for smoother user experience)

Drawer drawer;
drawer = (Drawer)getActivity().getSupportFragmentManager().findFragmentById(R.id.theid);
drawer.mDrawerLayout.closeDrawer(Gravity.END);
//End for right and Start for left

Hope it helps

silverFoxA
  • 4,549
  • 7
  • 33
  • 73