So add open and close strings with any values in your strings.xml and reference them in the ActionBarDrawerToggle arguments. Note: The ActionBarDrawerToggle need two strings resources which must be provided to describe the open/close drawer actions for accessibility services. Sync the toggle so that it knows when to animate between a hamburger icon and a back icon.Add the toggle as a listener to our DrawerLayout so that it can animate the toggle upon opening and closing the drawer.Add an ActionBarDrawerToggle, which displays the hamburger icon in the toolbar.Create a new drawer variable for the DrawerLayout we added in XML. To add the navigation drawer, while still inside onCreate(), we need to do a couple of things to make it work: Note: Running the app at this time should show the title of the app in your toolbar.ģ. Add this line after setContentView() to setup our own toolbar.To breathe life into t he components you just added: I’ve only done it to make the app a bit visually appealing.Īt this point you’re done with the XML side of your app. Note: You might notice that you don’t have the icons and string resource for any of the items in the menu but the icons are okay to remove for now and give the title for each as you please. Inside the main_menu, add the following lines for a few items to be displayed inside the drawer: Add a new Menu Resource File inside res > menu directory (If you don’t already have the menu directory, create that directory under res)Ĩ. Note: At this point, you need a main_menu since that’s one thing that’s missing, which is basically a menu to display the items in the drawer, to create that:ħ. Open main_activity.xml again and below the LinearLayout, add the NavigationView (which is basically our Navigation Drawer): Replace your parent theme from to (which should solve the exception and running the app now should display an empty toolbar). Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.ĥ. If you run the project at this moment, you might run into an exception: : This Activity already has an action bar supplied by the window decor. It is because the default toolbar that’s set up by the project is from a parent layout that we do not have a control of, so the Navigation Drawer is always shown underneath the default toolbar when opened. Note: You might be thinking if we already have a toolbar set up, why add another. Set the parent layout ID to Inside the Drawer Layout, add the following code to add a Toolbar: Change the parent’s layout to .ĭrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.ģ.Note: At the time of writing, the latest Material version was 1.1.0, but you can use any latest stable release you want from Google’s Maven Repository or MVN Repository.ģ. Add this line in the dependency section:.Since Navigation Drawer is part of Android’s Material library, first thing is to add the Material library to our project. Run the project to see the look of your initial app. Type any name for the project and select Kotlin from the languageĥ. This would create an empty screen with an Action Bar.ģ.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |