Slider also known as Seekbar is used to get one value from a multiple range of values in both android and iOS applications. Slider is most likely used in applications to set Ringer volume, Media Volume, Notification Volume, Screen brightness etc. This tutorial is the simplest example of Slider . In this tutorial we would going to create a react native application with Text component and Slider component, Now when application user starts moving the Slider then it will automatically update and set value between 1 to 100 in TextView.
Contents in this project Seekbar Slider Android iOS 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 and Slider component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Slider} from ‘react-native’;
|
3. Create constructor() in your project’s class with super() method. Now create a State in your constructor named as SliderValue . Set the default starting value of SliderValue variable is zero(0) .
1
2
3
4
5
6
7
8
9
10
|
constructor(){
super();
this.state ={
SliderValue : 0
}
}
|
4. Create a Parent View in render’s return block and inside it create a Text component. Set the SliderValue State value in Text component. So when we will start updating the State value using Slider automatically updated value set inside the Text component.
1
2
3
4
5
|
<View style={styles.MainContainer}>
<Text style = {{fontSize: 20}}>Slider Value = { this.state.SliderValue }</Text>
</View>
|
5. Add Slider component just after the Text component.
step : This would set the margin value between each value.
minimumValue : Sets the minimum stating value of Slider .
maximumValue : Sets the maximum value of Slider .
minimumTrackTintColor : Color of Slider after moving.
onValueChange : Method call on moving Seekbar, so it will set the value inside Slider .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<View style={styles.MainContainer}>
<Text style = {{fontSize: 20}}>Slider Value = { this.state.SliderValue }</Text>
<Slider
step = { 1 }
minimumValue = { 0 }
maximumValue = { 100 }
minimumTrackTintColor = “#009688”
onValueChange={(ChangedValue) => this.setState({ SliderValue: ChangedValue })}
style = {{ width: ‘100%’ }}
/>
</View>
|
6. Create CSS Classes.
1
2
3
4
5
6
7
8
9
10
11
12
|
const styles = StyleSheet.create({
MainContainer :{
justifyContent: ‘center’,
alignItems: ‘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
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Slider} from ‘react-native’;
class Project extends Component {
constructor(){
super();
this.state ={
SliderValue : 0
}
}
render() {
return (
<View style={styles.MainContainer}>
<Text style = {{fontSize: 20}}>Slider Value = { this.state.SliderValue }</Text>
<Slider
step = { 1 }
minimumValue = { 0 }
maximumValue = { 100 }
minimumTrackTintColor = “#009688”
onValueChange={(ChangedValue) => this.setState({ SliderValue: ChangedValue })}
style = {{ width: ‘100%’ }}
/>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
margin: 10
}
});
AppRegistry.registerComponent(‘Project’, () => Project);
|
Screenshots in Android mobile app:
Screenshot in iOS mobile application :