Search code examples
androidnavigation-drawerdelaywait

how to use wait function in android?


In first run ,after opening the application it should wait for 1 second and open navigation drawer, after 1 second navigation drawer should close

SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(this);
    if (!preferences.getBoolean("Man",false))
    {

    // wait 1 second            

     DrawerLayout drawer = (DrawerLayout)  findViewById(R.id.drawer_layout);
        drawer.openDrawer(Gravity.LEFT);

   // wait 1 second

        DrawerLayout drawer = (DrawerLayout)  findViewById(R.id.drawer_layout);
        drawer.openDrawer(Gravity.RIGHT);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putBoolean("Man",true);
        editor.commit();
    }

Solution

  • You can use Handler to achieve wait time in your code

    Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        // Actions to do after 1 second
                    }
                }, 1000);