Alert is basic component available in react native. Alert component is used to display a simple alert message on both android and iOS applications screen. But using Alert we can also build our own Alert Dialog in react native applications with OK, CANCEL and ASK ME LATER button. The alert dialog is completely user modified and app developer himself can set its Title, Message, Ask Me Later button text, OK button text and Cancel button text.
Contents in this project Create Alert Dialog in Android iOS Application Example :
1. Start a fresh React Native project. If you don’t know how then read my this tutorial.
2. Import AppRegistry, StyleSheet, Text, View, Button and Alert component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Button, Alert } from ‘react-native’;
|
3. Create a function named as ShowAlertDialog() in your class. In this function we would create a Alert dialog message using Alert component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
ShowAlertDialog = () =>{
Alert.alert(
// This is Alert Dialog Title
‘Alert Dialog Title’,
// This is Alert Dialog Message.
‘Alert Dialog Message’,
[
// First Text Button in Alert Dialog.
{text: ‘Ask me later’, onPress: () => console.log(‘Ask me later Button Clicked’)},
// Second Cancel Button in Alert Dialog.
{text: ‘Cancel’, onPress: () => console.log(‘Cancel Button Pressed’), style: ‘cancel’},
// Third OK Button in Alert Dialog
{text: ‘OK’, onPress: () => console.log(‘OK ButtonPressed’)},
]
)
}
|
4. Create a Root parent View inside render’s return block.
1
2
3
4
5
6
7
8
9
10
11
|
render() {
return (
<View style={styles.MainContainer}>
</View>
);
}
|
5. Create Button component inside Parent View and call the ShowAlertDialog() message on button onPress event.
1
2
3
4
5
6
7
8
9
10
11
|
render() {
return (
<View style={styles.MainContainer}>
<Button title=“Show Alert Dialog “ onPress={this.ShowAlertDialog} />
</View>
);
}
|
6. Create style css class for View.
1
2
3
4
5
6
7
8
9
10
11
12
|
const styles = StyleSheet.create({
MainContainer :{
// Setting up View inside content in Vertically center.
justifyContent: ‘center’,
flex:1,
margin: 10
}
});
|
7. Complete source code for index.android.js / index.ios.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
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Button, Alert } from ‘react-native’;
class Myproject extends Component {
ShowAlertDialog = () =>{
Alert.alert(
// This is Alert Dialog Title
‘Alert Dialog Title’,
// This is Alert Dialog Message.
‘Alert Dialog Message’,
[
// First Text Button in Alert Dialog.
{text: ‘Ask me later’, onPress: () => console.log(‘Ask me later Button Clicked’)},
// Second Cancel Button in Alert Dialog.
{text: ‘Cancel’, onPress: () => console.log(‘Cancel Button Pressed’), style: ‘cancel’},
// Third OK Button in Alert Dialog
{text: ‘OK’, onPress: () => console.log(‘OK ButtonPressed’)},
]
)
}
render() {
return (
<View style={styles.MainContainer}>
<Button title=“Show Alert Dialog “ onPress={this.ShowAlertDialog} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
// Setting up View inside content in Vertically center.
justifyContent: ‘center’,
flex:1,
margin: 10
}
});
AppRegistry.registerComponent(‘Myproject’, () => Myproject);
|
Screenshot in iOS Application :
Screenshot in Android mobile application :