|
1 | | -# MaterialNavigationDrawerAndroid |
2 | | - Android Library to implement Rich, Beautiful Material Navigation View for your project with Material Design Guidelines. Easy to use. |
| 1 | +# Material Navigation View for Android 📱 |
| 2 | + |
| 3 | +📱 Android Library to implement Rich, Beautiful Material Navigation View for your project with Material Design Guidelines. Easy to use. |
| 4 | + |
| 5 | +<table style="width:100%"> |
| 6 | + <tr> |
| 7 | + <th><b>1. Round Right Corner Style<b></b></th> |
| 8 | + <th>2. Round Rectangle Style</th> |
| 9 | + |
| 10 | + </tr> |
| 11 | + <tr> |
| 12 | + <td><img src="Images/RoundRight.png"/></td> |
| 13 | + <td><img src="Images/RoundRect.png"/></td> |
| 14 | + </tr> |
| 15 | +</table> |
| 16 | + |
| 17 | + |
| 18 | +# Table of Contents: |
| 19 | +> - [ Introduction ](#introduction) |
| 20 | +> - [ Implementation ](#implementation) |
| 21 | +> - [ Prerequisite ](#prerequisite) |
| 22 | +> - [ Create `Activity.xml` ](#createActivityXML) |
| 23 | +> - [ Create Activity File](#createActivityCode) |
| 24 | +> |
| 25 | +> - [ Migrating from `NavigationView` ](#migrate) |
| 26 | +> - [ Fast Implementation ](#fastImplementation) |
| 27 | +> - [ Credits ](#credits) |
| 28 | +
|
| 29 | +<a name="introduction"></a> |
| 30 | +## Introduction |
| 31 | +**MaterialNavigationView** library is built upon Google's Material Design library. This API will be useful to create rich, animated, beautiful Navigation View Drawer in Android app easily. |
| 32 | +It follows all Material Design Guidelines as stated [here](https://material.io) <br> |
| 33 | +`MaterialNavigationView` class in this library is inherited from [`com.google.android.material.navigation.NavigationView`](https://github.com/material-components/material-components-android/blob/master/docs/components/NavigationView.md) class. Just only difference is added extra design. <br> |
| 34 | +So, we can use it as it is. |
| 35 | + |
| 36 | +<a name="implementation"></a> |
| 37 | +## Implementation |
| 38 | +Implementation of Material Navigation View library is so easy. You can check [/app](/app) directory for demo. Let's have look on basic steps of implementation. |
| 39 | +In this demo, we will populate The menu contents by a menu resource file. |
| 40 | +<a name="prerequisite"></a> |
| 41 | +### Prerequisite |
| 42 | +#### Gradle |
| 43 | +In `Build.gradle` of app module, include these dependencies. If you want to show animations, include *Lottie* animation library. |
| 44 | +```groovy |
| 45 | +dependencies { |
| 46 | +
|
| 47 | + // Material Navigation View Library |
| 48 | + implementation 'com.shreyaspatil:MaterialNavigationView:1.0.0' |
| 49 | +
|
| 50 | + // Material Design Library |
| 51 | + implementation 'com.google.android.material:material:1.0.0' |
| 52 | +} |
| 53 | +``` |
| 54 | +#### Set up Material Theme |
| 55 | +Setting Material Theme to app is necessary before implementing Material Navigation View library. To set it up, update [`styles.xml`](app\src\main\res\values\styles.xml) of `values` directory in app. |
| 56 | +`colorSecondary` value is important here because this color is applied to menu item of Navigation View. |
| 57 | +```xml |
| 58 | +<resources> |
| 59 | + <style name="AppTheme" parent="Theme.MaterialComponents.Light"> |
| 60 | + <!-- Customize your theme here. --> |
| 61 | + <item name="colorPrimary">@color/colorPrimary</item> |
| 62 | + <item name="colorPrimaryDark">@color/colorPrimaryDark</item> |
| 63 | + <item name="colorAccent">@color/colorAccent</item> |
| 64 | + |
| 65 | + <!-- colorSecondary will be applied to Menu item of NavigationView --> |
| 66 | + <item name="colorSecondary">@color/colorPrimary</item> |
| 67 | + ... |
| 68 | + </style> |
| 69 | +</resources> |
| 70 | +``` |
| 71 | +These are required prerequisites to implement Material Navigation View library. |
| 72 | + |
| 73 | +<a name="createActivityXML"></a> |
| 74 | +### Create Activity XML |
| 75 | +This is most commonly used in conjunction with `DrawerLayout` to implement Material navigation drawers. Navigation drawers are modal elevated dialogs that come from the start/left side, used to display in-app navigation links. <br> |
| 76 | +NavigationView is a scrollable view that renders a menu resource `(R.menu.<something>)` as a vertical list. It also renders a header view above the menu. <br> |
| 77 | +We are creating `activity_main.xml` |
| 78 | +```xml |
| 79 | +<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 80 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 81 | + xmlns:tools="http://schemas.android.com/tools" |
| 82 | + android:id="@+id/drawer_layout" |
| 83 | + android:layout_width="match_parent" |
| 84 | + android:layout_height="match_parent" |
| 85 | + android:fitsSystemWindows="true" |
| 86 | + tools:openDrawer="start"> |
| 87 | + |
| 88 | + <!-- Other Stuff here --> |
| 89 | + |
| 90 | + <com.shreyaspatil.material.navigationview.MaterialNavigationView |
| 91 | + android:id="@+id/nav_view" |
| 92 | + android:layout_width="wrap_content" |
| 93 | + android:layout_height="match_parent" |
| 94 | + app:itemStyle="rounded_right" |
| 95 | + app:menu="@menu/activity_main_drawer" /> |
| 96 | + |
| 97 | +</androidx.drawerlayout.widget.DrawerLayout> |
| 98 | +``` |
| 99 | +#### Available Flags |
| 100 | +As already mentioned, this class is inherited from `NavigationView`. You can use all exisiting flags of that class. <br> |
| 101 | +New important flag here is. |
| 102 | +- `itemStyle` - Points to a style of menu item of Navigation drawer. <br> |
| 103 | +There are currently 2 menu styles are defined as below |
| 104 | + - `rounded_right`: This flag sets design to menu item of Navigation drawer as **Rounded Corners at right** as you can see below. |
| 105 | +  |
| 106 | + |
| 107 | + Implemented as follows: |
| 108 | + ```xml |
| 109 | + <com.shreyaspatil.material.navigationview.MaterialNavigationView |
| 110 | + ... |
| 111 | + app:itemStyle="rounded_right"/> |
| 112 | + ``` |
| 113 | + |
| 114 | + - `rounded_rectangle`: This flag sets design to menu item of Navigation drawer as **Rounded Rectangular Corners** as you can see below. |
| 115 | +  |
| 116 | + |
| 117 | + Implemented as follows: |
| 118 | + ```xml |
| 119 | + <com.shreyaspatil.material.navigationview.MaterialNavigationView |
| 120 | + ... |
| 121 | + app:itemStyle="rounded_rectangle"/> |
| 122 | + ``` |
| 123 | +<a name="createActivityCode"></a> |
| 124 | +### Create Activity Code (Java/Kotlin) |
| 125 | +All the programmatic way of implementation of `MaterialNavigationView` is same as `NavigationView`. Just change is the class name only. <br> |
| 126 | +Two methods are added in this new class as follows.. |
| 127 | +- `setItemStyle(int itemStyle)` : This method sets the Item Style of Menu in MaterialNavigationView at runtime. <br> |
| 128 | + `itemStyle` should be one of the following constants : |
| 129 | + - `MaterialNavigationView.ITEM_STYLE_ROUND_RIGHT` |
| 130 | + - `MaterialNavigationView.ITEM_STYLE_ROUND_RECTANGLE` |
| 131 | +- `getItemStyle()` : It returns the value of item style of menu. |
| 132 | + |
| 133 | +Here is a demo of `MaterialNavigationView` in which we will switch item style of NavigationView after selecting menu. |
| 134 | + |
| 135 | +```kotlin |
| 136 | +class MainActivity : AppCompatActivity() { |
| 137 | + private lateinit var navView: MaterialNavigationView |
| 138 | + |
| 139 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 140 | + super.onCreate(savedInstanceState) |
| 141 | + setContentView(R.layout.activity_main) |
| 142 | + ... |
| 143 | + navView = findViewById(R.id.nav_view) |
| 144 | + ... |
| 145 | + } |
| 146 | + |
| 147 | + override fun onOptionsItemSelected(item: MenuItem): Boolean { |
| 148 | + val itemStyle = when (item.itemId) { |
| 149 | + R.id.action_round_rect -> MaterialNavigationView.ITEM_STYLE_ROUND_RECTANGLE |
| 150 | + R.id.action_round_right -> MaterialNavigationView.ITEM_STYLE_ROUND_RIGHT |
| 151 | + else -> MaterialNavigationView.ITEM_STYLE_ROUND_RIGHT |
| 152 | + } |
| 153 | + navView.setItemStyle(itemStyle) |
| 154 | + |
| 155 | + return false |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +``` |
| 160 | + |
| 161 | +<a name="migrate"></a> |
| 162 | +## Migrating from `NavigationView` |
| 163 | +If you are already using `NavigationView` (`com.google.android.material.navigation.NavigationView`) in your project and want to switch it to `MaterialNavigationView` then you are done! <br> |
| 164 | +Do following Changes: |
| 165 | +- Change in layout file - Just change package of component from `com.google.android.material.navigation.NavigationView` to `com.shreyaspatil.material.navigationview.MaterialNavigationView` wherever you used it. |
| 166 | +- Change in Activity Code - Just change `NavigationView` class to `MaterialNavigationView` and import appropriate package. |
| 167 | + |
| 168 | +:confetti_ball: Hurrah! you are done and successfully migrated to `MaterialNavigationView`. Now just run your app and see magic. |
| 169 | +<a name="credits"></a> |
| 170 | + |
| 171 | +<a name="fastImplementation"></a> |
| 172 | +## Fast Implementation |
| 173 | +Want to use it fast? Then here it is..<br> |
| 174 | +In Android Studio, Right Click -> `New` -> `Activity` -> `Navigation Drawer Activity` and done. |
| 175 | +Then Change just package in layout file and class name in Activity code file and you're done. Run your app and see magic :rocket: |
| 176 | + |
| 177 | + |
| 178 | +## Credits |
| 179 | +This library is built using following open-source libraries. |
| 180 | +- [Material Components for Android](https://github.com/material-components/material-components-android) |
| 181 | + |
| 182 | +If you like this library, Please start this repo and share with someone who need it. You can contribute if you have any suggestions or ideas to improve it. |
0 commit comments