-1

I opened an navigation drawer onCreate() of my main activity. I also want to close that navigation drawer after 1 or 2 second showing.

I tried

if(navDrawer.isHovered()){
       navDrawer.closeDrawers();
    }

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setToolbarContent(imgNotification,txtNotification);
        fragmentManager = getSupportFragmentManager();
        navDrawer.openDrawer(rightNavigationView,true);
        if(navDrawer.isHovered()){
           navDrawer.closeDrawers();
        }
    }

I want to know how I can set an timer for the closing of the navigation drawer. Is there any way.

Edric
  • 24,639
  • 13
  • 81
  • 91
  • start post delay when drawer opened and check is drawer opened – Rohit Sep 09 '19 at 11:50
  • Try this https://stackoverflow.com/questions/4597690/android-timer-how-to – Krystian G Sep 09 '19 at 11:51
  • 1
    Hi Shahab, welcome to StackOverflow! Please note that it's not really a good idea to automatically close the drawer after a certain amount of time has passed as this can be a bad accessibility approach especially for users who are slow readers/take a while to decide what to click on. (P.S. Is there any valid reason why you want to close the drawers after a certain amount of time?) – Edric Sep 09 '19 at 11:51
  • 1
    @Rohit Thanks I tried that approach and it worked. – A. K. M. Shahab Uddin Sep 11 '19 at 04:08
  • @AKMShahabUddin accept answer if that helped – Rohit Sep 12 '19 at 05:27

3 Answers3

1

Try this to close after 2 second

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            navDrawer.closeDrawers();
        }
    },2000);
Ankit
  • 1,068
  • 6
  • 10
1

Step 1: Add a Listener of Nav Drawer , in opened call back implement this

@Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }

implement this code into this mehtod

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
if(navDrawer.isOpened()){
        navDrawer.closeDrawers();
}
    }
},2000);
Rohit
  • 3,401
  • 4
  • 33
  • 60
1

Don't just create random handler objects, because that might cause issues like randomly closing of drawer due to pending instructions (runnables). You can directly post/post-delay runnables to views. e.g:-

class ExampleActivity extends AppCompatActivity {

    private static final long AUTO_CLOSE_DELAY = 2000L;

    @Nullable
    private DrawerLayout drawerLayout;

    @NonNull
    private final Runnable closeDrawerRunnable = new Runnable() {
        @Override
        public void run() {
            if(drawerLayout!=null && drawerLayout.isDrawerOpen(GravityCompat.START)){
                drawerLayout.closeDrawer(GravityCompat.START);
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
        drawerLayout = findViewById(R.id.drawerLayout);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                drawerLayout.removeCallbacks(closeDrawerRunnable);
                drawerLayout.postDelayed(closeDrawerRunnable,AUTO_CLOSE_DELAY);
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                drawerLayout.removeCallbacks(closeDrawerRunnable);
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }
}
Harsh Jatinder
  • 833
  • 9
  • 15