Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I open the drawer layout with animation programmatically?

Tags:

I created the app drawer by using the following library: http://developer.android.com/training/implementing-navigation/nav-drawer.html

I want to show the Navigation Drawer with animation when opening the app. How can I do that?

like image 498
benleung Avatar asked Aug 06 '13 02:08

benleung


People also ask

Which layout is used for navigation drawer?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How do you use DrawerLayout?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.


2 Answers

Predraw listener, aka the safeway

Here is the predraw listener example. It will literally start the animation as soon as it can which maybe a little too fast. You might want to do a combination of this with a runnable shown second. I will not show the two combined, only separate.

@Override protected void onCreate(Bundle savedInstanceState) {     ...     // Building NavDrawer logic here. Just a method call would be best.     ...      ViewTreeObserver vto = drawer.getViewTreeObserver();     if (vto != null) vto.addOnPreDrawListener(new ShouldShowListener(drawer)); }  private static class ShouldShowListener implements OnPreDrawListener {      private final DrawerLayout drawerLayout;      private ShouldShowListener(DrawerLayout drawerLayout) {         this.drawerLayout= drawerLayout;     }      @Override     public boolean onPreDraw() {         if (view != null) {             ViewTreeObserver vto = view.getViewTreeObserver();             if (vto != null) {                 vto.removeOnPreDrawListener(this);             }         }          drawerLayout.openDrawer(Gravity.LEFT);         return true;     } } 

PostDelay Runnable, aka living dangerous

// Delay is in milliseconds static final int DRAWER_DELAY = 200;  @Override protected void onCreate(Bundle savedInstanceState) {     ...     // Building NavDrawer logic here. Just a method call would be best.     ...     new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY); }  private Runnable openDrawerRunnable() {     return new Runnable() {          @Override         public void run() {             drawerLayout.openDrawer(Gravity.LEFT);         }     } } 

WARNING

If they rotate on the start of the app for the first time BOOM! Read this blog post for more information http://corner.squareup.com/2013/12/android-main-thread-2.html. Best thing to do would be to use the predraw listener or remove your runnable in onPause.

like image 73
MinceMan Avatar answered Sep 29 '22 05:09

MinceMan


You can call openDrawer(int gravity) on the DrawerLayout to make it open the drawer with an animation.

like image 42
Brayden Avatar answered Sep 29 '22 06:09

Brayden