Drawer Navigator also known as Navigation drawer is one of the most useful infrastructure to hold activities in both android and iOS applications. Drawer Navigator manages all the screens into sequential way with a customized sidebar menu panel. We have already make a tutorial on Drawer Navigator but in our previous tutorial we haven’t create the customized sidebar menu panel so this tutorial is the second part of Drawer Navigator tutorial. In this tutorial we would going to create fully customized Drawer Navigator with Custom Sidebar Menu with Icons Tutorial in react native Android iOS Example.
Contents in this project Drawer Navigator with Custom Sidebar Menu with Icons React Native iOS Android Example Tutorial:
Note: Before getting started you need to read our previous tutorial on Drawer Navigator , In this tutorial i have explained all the contents or navigational drawer .
1. First step is to install the React Navigation library in our current react native project, This step is must because using the react navigation library we can use Drawer Navigator component in our current project including Activities. So open your current react native current project folder in command prompt(Terminal) and execute below command.
1
|
npm install —save react–navigation
|
Screenshot:
Screenshot of CMD after successfully installed the library:
2. Import StyleSheet, Platform, View, Text, Image, TouchableOpacity, YellowBox and Dimensions component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, View, Text, Image, TouchableOpacity, YellowBox, Dimensions } from ‘react-native’;
|
3. Import DrawerNavigator component from react navigation library.
1
|
import { DrawerNavigator } from ‘react-navigation’;
|
4. Import StackNavigator component from react navigation library into your project. Without the StackNavigator we cannot use the multiple activities into your project.
1
|
import { StackNavigator } from ‘react-navigation’
|
5. Create a new class in your App.js file named as HamburgerIcon . This class is used to put a Hamburger Icon into our navigational drawer.
toggleDrawer : This function is used to open and close the drawer navigator with its own this.props.navigationProps.toggleDrawer() inbuilt function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class HamburgerIcon extends Component {
toggleDrawer = () => {
console.log(this.props.navigationProps);
this.props.navigationProps.toggleDrawer();
}
render() {
return (
<View style={{ flexDirection: ‘row’ }}>
<TouchableOpacity onPress={this.toggleDrawer.bind(this)} >
<Image
source={{ uri: ‘/wp-content/uploads/2018/04/hamburger_icon.png’ }}
style={{ width: 25, height: 25, marginLeft: 5 }}
/>
</TouchableOpacity>
</View>
);
}
}
|
6. Create a class named as Custom_Side_Menu . This class is our main drawer navigator side menus class, Inside this class we are creating all the menus we have wanted to show inside the side panel. If you want to modify the menus structure as your requirement then simply edit this class and put your design inside render’s return block.
this.props.navigation.navigate(”) : We are calling this inbuilt function on each menu item clicked, as you can see we pass a argument inside this function as First , Second and Third which would represents as our all 3 activities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
class Custom_Side_Menu extends Component {
render() {
return (
<View style={styles.sideMenuContainer}>
<Image source={{ uri: ‘/wp-content/uploads/2017/10/Guitar.jpg’ }}
style={styles.sideMenuProfileIcon} />
<View style={{ width: ‘100%’, height: 1, backgroundColor: ‘#e2e2e2’, marginTop: 15}} />
<View style={{width: ‘100%’}}>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/social.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘First’) }} > First Activity </Text>
</View>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/promotions.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘Second’) }} > Second Activity </Text>
</View>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/outbox.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘Third’) }} > Third Activity </Text>
</View>
</View>
<View style={{ width: ‘100%’, height: 1, backgroundColor: ‘#e2e2e2’, marginTop: 15}} />
</View>
);
}
}
|
Screenshot of Side Menu panel:
7. Create a new Activity class named as MainActivity. This is our main home screen activity .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class MainActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 1 </Text>
</View>
);
}
}
|
Screenshot of MainActivity:
8. Create a new class named as SecondActivity, This would be our second activity screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class SecondActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 2 </Text>
</View>
);
}
}
|
Screenshot of Second Activity class:
9. Create a new activity class named as ThirdActivity, this would be our third and final activity in this project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class ThirdActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 3 </Text>
</View>
);
}
}
|
Screenshot of Third Activity:
10. Create 3 Constant objects named as FirstActivity_StackNavigator, SecondActivity_StackNavigator and ThirdActivity_StackNavigator. Inside each of them we would call the Hamburger Icon and define the Activity name and activity header style. This is used to call the activities from drawer navigator panel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
const FirstActivity_StackNavigator = StackNavigator({
First: {
screen: MainActivity,
navigationOptions: ({ navigation }) => ({
title: ‘MainActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
const SecondActivity_StackNavigator = StackNavigator({
Second: {
screen: SecondActivity,
navigationOptions: ({ navigation }) => ({
title: ‘SecondActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
const ThirdActivity_StackNavigator = StackNavigator({
Third: {
screen: ThirdActivity,
navigationOptions: ({ navigation }) => ({
title: ‘ThirdActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
|
11. Create the DrawerNavigator calling panel and call all above three activities constants inside them .
contentComponent : Calling the Custom_Side_Menu class .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
export default MyDrawerNavigator = DrawerNavigator({
MainStack: {
screen: FirstActivity_StackNavigator
},
SecondStack: {
screen: SecondActivity_StackNavigator
},
ThirdStack: {
screen: ThirdActivity_StackNavigator
}
},
{
contentComponent: Custom_Side_Menu,
drawerWidth: Dimensions.get(‘window’).width – 130,
});
|
12. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
alignItems: ‘center’,
justifyContent: ‘center’,
},
sideMenuContainer: {
width: ‘100%’,
height: ‘100%’,
backgroundColor: ‘#fff’,
alignItems: ‘center’,
paddingTop: 20
},
sideMenuProfileIcon:
{
resizeMode: ‘center’,
width: 150,
height: 150,
borderRadius: 150/2
},
sideMenuIcon:
{
resizeMode: ‘center’,
width: 28,
height: 28,
marginRight: 10,
marginLeft: 20
},
menuText:{
fontSize: 15,
color: ‘#222222’,
}
});
|
13. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, View, Text, Image, TouchableOpacity, YellowBox, Dimensions } from ‘react-native’;
import { DrawerNavigator } from ‘react-navigation’;
import { StackNavigator } from ‘react-navigation’
class HamburgerIcon extends Component {
toggleDrawer = () => {
console.log(this.props.navigationProps);
this.props.navigationProps.toggleDrawer();
}
render() {
return (
<View style={{ flexDirection: ‘row’ }}>
<TouchableOpacity onPress={this.toggleDrawer.bind(this)} >
<Image
source={{ uri: ‘/wp-content/uploads/2018/04/hamburger_icon.png’ }}
style={{ width: 25, height: 25, marginLeft: 5 }}
/>
</TouchableOpacity>
</View>
);
}
}
class Custom_Side_Menu extends Component {
render() {
return (
<View style={styles.sideMenuContainer}>
<Image source={{ uri: ‘/wp-content/uploads/2017/10/Guitar.jpg’ }}
style={styles.sideMenuProfileIcon} />
<View style={{ width: ‘100%’, height: 1, backgroundColor: ‘#e2e2e2’, marginTop: 15}} />
<View style={{width: ‘100%’}}>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/social.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘First’) }} > First Activity </Text>
</View>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/promotions.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘Second’) }} > Second Activity </Text>
</View>
<View style={{flexDirection: ‘row’, alignItems: ‘center’, marginTop: 10}}>
<Image source={{ uri: ‘/wp-content/uploads/2018/08/outbox.jpg’ }}
style={styles.sideMenuIcon} />
<Text style={styles.menuText} onPress={() => { this.props.navigation.navigate(‘Third’) }} > Third Activity </Text>
</View>
</View>
<View style={{ width: ‘100%’, height: 1, backgroundColor: ‘#e2e2e2’, marginTop: 15}} />
</View>
);
}
}
class MainActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 1 </Text>
</View>
);
}
}
class SecondActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 2 </Text>
</View>
);
}
}
class ThirdActivity extends Component {
constructor(props) {
super(props);
YellowBox.ignoreWarnings([
‘Warning: componentWillMount is deprecated’,
‘Warning: componentWillReceiveProps is deprecated’,
]);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 23 }}> This is Activity – 3 </Text>
</View>
);
}
}
const FirstActivity_StackNavigator = StackNavigator({
First: {
screen: MainActivity,
navigationOptions: ({ navigation }) => ({
title: ‘MainActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
const SecondActivity_StackNavigator = StackNavigator({
Second: {
screen: SecondActivity,
navigationOptions: ({ navigation }) => ({
title: ‘SecondActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
const ThirdActivity_StackNavigator = StackNavigator({
Third: {
screen: ThirdActivity,
navigationOptions: ({ navigation }) => ({
title: ‘ThirdActivity’,
headerLeft: <HamburgerIcon navigationProps={navigation} />,
headerStyle: {
backgroundColor: ‘#FF9800’
},
headerTintColor: ‘#fff’,
})
},
});
export default MyDrawerNavigator = DrawerNavigator({
MainStack: {
screen: FirstActivity_StackNavigator
},
SecondStack: {
screen: SecondActivity_StackNavigator
},
ThirdStack: {
screen: ThirdActivity_StackNavigator
}
},
{
contentComponent: Custom_Side_Menu,
drawerWidth: Dimensions.get(‘window’).width – 130,
});
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
alignItems: ‘center’,
justifyContent: ‘center’,
},
sideMenuContainer: {
width: ‘100%’,
height: ‘100%’,
backgroundColor: ‘#fff’,
alignItems: ‘center’,
paddingTop: 20
},
sideMenuProfileIcon:
{
resizeMode: ‘center’,
width: 150,
height: 150,
borderRadius: 150/2
},
sideMenuIcon:
{
resizeMode: ‘center’,
width: 28,
height: 28,
marginRight: 10,
marginLeft: 20
},
menuText:{
fontSize: 15,
color: ‘#222222’,
}
});
|
Screenshots: