Radio Buttons are graphical round shape elements that allows the application user to select only one single item from given multiple items. Radio Button groups is group of multiple radio buttons works in a singular way. When application user select radio button then all the other radio buttons will automatically de-selected, so app user can select one item at a time. In react native there is yet not a specific Custom Radio Button Group is available but using the custom NPM GitHub libraries we can easily make our own radio button groups. We are using the react-native-btr NPM library in our project. This library is awesome guys.
Contents in this project Create Custom Radio Button Group in React Native Example:
1. Open your react native project folder in Command Prompt or Terminal & execute below command to install the react-native-btr library in your project.
1
|
npm i react–native–btr —save
|
Screenshot of terminal:
2. Import StyleSheet, View, Platform and Text component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Platform, Text } from ‘react-native’;
|
3. Import RadioGroup component from react-native-btr library.
1
|
import { RadioGroup } from ‘react-native-btr’;
|
4. Create constructor() method in your project and in the constructor make a State array named as radioButtons with 3 objects. Each object will make an individual radio button.
label : Define the radio button name which shows on screen.
value : Define which value app user wants to access on selection of radio button.
checked : Supports true and false Boolean type values. If you set true then it will by default active the current radio button.
color : Color of radio button label text.
disabled : To disable the current radio button, User won’t be able to select.
flexDirection : Supports all default flex direction values.
size : Size of Radio button.
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
|
constructor() {
super();
this.state = {
radioButtons: [
{
label: ‘Apple’,
value: ‘Apple’,
checked: true,
color: ‘#F44336’,
disabled: false,
flexDirection: ‘row’,
size: 11
},
{
label: ‘Mango’,
value: ‘Mango’,
checked: false,
color: ‘#FF8F00’,
disabled: false,
flexDirection: ‘row’,
size: 11
},
{
label: ‘Banana’,
value: ‘Banana’,
checked: false,
color: ‘#1B5E20’,
disabled: false,
flexDirection: ‘row’,
size: 11
}
]
}
}
|
5. Creating a selectedItem variable in render’s block. We would use the JavaScript’s .find() method to find the true value of checked and after finding the true value object it will store the selected radio button value in variable.
1
2
|
let selectedItem = this.state.radioButtons.find(e => e.checked == true);
selectedItem = selectedItem ? selectedItem.value : this.state.radioButtons[0].value;
|
6. Create the RadioGroup component in render’s return block with 1 Text component.
color : Used to set the color of Radio button.
labelStyle : Supports all text styles.
onPress : Updating the complete state value.
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
|
render() {
let selectedItem = this.state.radioButtons.find(e => e.checked == true);
selectedItem = selectedItem ? selectedItem.value : this.state.radioButtons[0].value;
return (
<View style={styles.MainContainer}>
<RadioGroup
color=‘#0277BD’
labelStyle={{ fontSize: 14, }}
radioButtons={this.state.radioButtons}
onPress={radioButtons => this.setState({ radioButtons })}
style={{ paddingTop: 20 }}
/>
<View style={styles.selectedItemView}>
<Text style={styles.selectedText}>Selected Item: {selectedItem}</Text>
</View>
</View>
);
}
|
7. Create 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
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
backgroundColor: ‘#FFF8E1’,
justifyContent: ‘center’,
alignItems: ‘center’,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
},
selectedItemView:
{
position: ‘absolute’,
left: 0,
right: 0,
bottom: 0,
padding: 14,
backgroundColor: ‘#263238’,
justifyContent: ‘center’,
alignItems: ‘center’
},
selectedText:
{
fontSize: 17,
color: ‘#fff’
}
});
|
8. 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
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Platform, Text } from ‘react-native’;
import { RadioGroup } from ‘react-native-btr’;
export default class App extends Component<{}> {
constructor() {
super();
this.state = {
radioButtons: [
{
label: ‘Apple’,
value: ‘Apple’,
checked: true,
color: ‘#F44336’,
disabled: false,
flexDirection: ‘row’,
size: 11
},
{
label: ‘Mango’,
value: ‘Mango’,
checked: false,
color: ‘#FF8F00’,
disabled: false,
flexDirection: ‘row’,
size: 11
},
{
label: ‘Banana’,
value: ‘Banana’,
checked: false,
color: ‘#1B5E20’,
disabled: false,
flexDirection: ‘row’,
size: 11
}
]
}
}
render() {
let selectedItem = this.state.radioButtons.find(e => e.checked == true);
selectedItem = selectedItem ? selectedItem.value : this.state.radioButtons[0].value;
return (
<View style={styles.MainContainer}>
<RadioGroup
color=‘#0277BD’
labelStyle={{ fontSize: 14, }}
radioButtons={this.state.radioButtons}
onPress={radioButtons => this.setState({ radioButtons })}
style={{ paddingTop: 20 }}
/>
<View style={styles.selectedItemView}>
<Text style={styles.selectedText}>Selected Item: {selectedItem}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
backgroundColor: ‘#FFF8E1’,
justifyContent: ‘center’,
alignItems: ‘center’,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0,
},
selectedItemView:
{
position: ‘absolute’,
left: 0,
right: 0,
bottom: 0,
padding: 14,
backgroundColor: ‘#263238’,
justifyContent: ‘center’,
alignItems: ‘center’
},
selectedText:
{
fontSize: 17,
color: ‘#fff’
}
});
|
Screenshots :