Unit 6 - React Native With Android
Unit 6 - React Native With Android
6.1 INTRODUCTION
6.2 ENVIRONMENT SETUP
6.3 JAVASCRIPT ES6 OVERVIEW
UNIT 6
6.4 CREATE REACT NATIVE APP
6.5 REACT NATIVE ALERT API
6.6 REACT NATIVE GEOLOCATION API
6.7 THIRD PART Y LIBRARIES
React Native lets you build mobile apps using only JavaScript .
It uses the same design as React, letting you compose a rich
mobile UI from declarative components.
With React Native, you don't build a mobile web app, an
HTML5 app, or a hybrid app; you build a real mobile app that's
indistinguishable from an app built using Objective -C or Java.
React Native uses the same fundamental UI building blocks as
regular iOS and Android apps. You just put those building
blocks together using JavaScript and React.
REACT NATIVE FEATURES
@"%SystemRoot%\System32\WindowsPowerShell\v1 .0\power
shell.exe" -NoProfile -InputFormat None -ExecutionPolicy
Bypass -Command "iex ((New -Object
System.Net.WebClient).DownloadString('https://chocolatey.or
g/install.ps1'))" && SET
"PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
React Native Environment Setup
3. Open your Command Prompt in Administrative mode (right-
click Command Prompt and select "Run as Administrator"),
paste this code and click enter. It will install Chocolatey .
4. In the Administrative mode of command prompt, paste the
code and run it (install Node.js, Python, and JDK).
If you have already installed Node.js, make sure it has version
above 8 and if you have already installed JDK, make it version
8 or newer version.
choco install -y nodejs.install python2 jdk8
Node comes with npm (Node Package Manager), which lets
you install the React Native. Run the given code:
C:\Users\Rakesh\AppData\Lo
cal\Android\Sdk
3. Install the required components, go to Tools Android > SDK
Manager > SDK platforms and select the required components
and install.
REACT NATIVE FIRST APPLICATION HELLO
WORLD
Steps to create React Native application
1 . First, you have to start your emulator (Android Emulator)
and make it live.
2. Create a directory ( ReactNative) in your any drive.
3. Open "Command Prompt" and go to your ReactNative
directory.
4. Write a command react -native init FirstApp to initialize your
app "FirstApp".
5. Go to your directory location " FirstApp" and run the
command react-native run-android. It will start Node
server and launch your application in virtual device emulator.
View Code of React Native Application
Open your one of the favorite JavaScript supportable IDE and
open App.js file inside your FirstApp application.
HELLO WORLD" APPLICATION
App.js
impor t React, {Component} from 'react';
impor t {Platform, StyleSheet, Text, View} from 'react -native';
render() {
return (
<View style={ styles.container }>
<View style={ styles.buttonContainer }>
<Button
onPress={this.onPressButton }
title="Press Me"
/>
</View>
<View style={ styles.buttonContainer }>
<Button
onPress={this.onPressButton }
title="Press Me"
color="#009933"
/>
< / V i ew >
< V i ew s t y l e = { s t y l e s .m ul t i B ut to n C o n t a i n e r } >
< B u t to n
o n P r e s s = { t hi s . o n P r e s s B ut to n }
t i t l e = " A d i s a b l e d b u t to n "
d i s a b l e d = { t r ue }
/>
< B u t to n
o n P r e s s = { t hi s . o n P r e s s B ut to n }
t i t l e = " OK !"
c o l o r = "# 0 0 9 9 3 3 "
/>
< / V i ew >
< / V i ew >
); } }
c o n s t s t y l e s = S t y l e S h e et . c r e a te ( {
container: {
f l ex : 1 ,
j u s t i f y C o n te n t : ' c e n te r ' ,
},
b u t ton C o n t ai n e r : {
margin: 20
},
m u l t iB ut to n C o n t a i n e r : {
margin: 20,
f l ex D ir ec t i o n : ' r o w ' ,
j u s t i f y C o n te n t : ' s p a c e - b et we e n '
}
})
OUTPUT
React Native Code Explanation
import React, {Component} from 'react': imports the library
and other components from react module and assign then to
variable React.
const instruction: sets to display the platform-specific
message.
export default class App extends Component: defines the
classes that extend the React Component. The export default
class modifier makes the class "public". This block of code
defines the components that represent the user interface.
render(): a function that returns a React element.
return(): returns the result of layout and UI components.
View: a container that supports the layout accessibility
controls. It is a fundamental component for building the UI .
Text: a React component for displaying text.
style: a property used for styling the components using
StyleSheet.
styles: used to design individual components.
{styles.instructions}>{instructions}:
const styles = StyleSheet.create({}): The StyleSheet class
creates the style object that controls the layout and
appearance of components. It is similar to Cascading Style
Sheets (CSS) used on the Web.
REACT NATIVE ALERT
e x p o r t d e f a u l t c l a s s B u t to n B a s i c s e x te n d s C o m p o n e n t {
showAlert1() {
Alert.aler t(
'Alert Title',
'My Alert Msg',
[
{
te x t : ' C a n c e l ' ,
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{ te x t : ' O K ' , o n P r e s s : ( ) = > c o n s o l e . l o g ( ' O K P r e s s e d ' ) } ,
] ); }
showAlert2() {
Alert.aler t(
'Alert Title',
'My Alert Msg',
[
{ te x t : ' A s k m e l a te r ' , o n P r e s s : ( ) = > c o n s o l e . l o g ( ' A s k m e l a te r p r e s s e d ' ) } ,
{
te x t : ' C a n c e l ' ,
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{ te x t : ' O K ' , o n P r e s s : ( ) = > c o n s o l e . l o g ( ' O K P r e s s e d ' ) } ,
],
{cancelable: false}
) }
render() {
return (
<View style={styles.container }>
<View style={styles.buttonContainer }>
< B u t to n
o n P r e s s = { t h i s . s h ow A l e r t 1 }
title="Button 1"
/>
</View>
<View style={styles.buttonContainer }>
< B u t to n
o n P r e s s = { t h i s . s h ow A l e r t 2 }
title="Button 2"
color="#009933"
/>
</View>
</View>
); } }
Method Description
getCurrentPosition() It invokes the success callback once with latest
location information.
watchPosition() It invokes the success callback whenever the
location changes. It returns a watchId (number).
this.setState({
ready:true,
where: {lat: position.coords.latitude,lng:position.coords.longitude
}
})
}
geoFailure = (err) => {
this.setState({error: err.message});
}
CONT…2
render() {
return (
<View style={ styles.container }>
{ !this.state.ready && (
<Text style={ styles.big }>Using Geolocati on in React
Native.</Text>
)}
{ this.state.error && (
<Text style={ styles.big }>Error: {this.state.error }</Text>
)}
{ this.state.ready && (
<Text style={ styles.big }>
Latitude: { this.state.where.lat }
Longitude: { this.state.where.lng }
</Text>
)}
</View>
);
}
}
CONT…3
import com.oblador.vectoricons.VectorIconsPackage ;
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new VectorIconsPackage ()
);
}
To add more libraries, simply separate them with comma and
add libraries packages. The above procedures are common to
add native libraries in the Android.
i m p o r t Re a c t , { C o m p o n e n t } f r o m ' r e a c t ' ;
i m p o r t { A p p Re g i s t r y, Tex t , Tex t I n p ut , V i ew } f r o m ' r e a c t - n a t i ve ' ;
ex p o r t d e f a u l t c l a s s P i z z a Tr a n s l a to r ex te n d s C o m p o n e n t {
c o n s t r uc tor (p r o p s ) {
s u p e r ( p ro p s ) ;
t h i s . s t a te = { tex t : ' ' } ;
}
render() {
r et u r n (
< V i ew s t y l e = { { p a d d i n g : 1 0 } } >
< Tex t I n p ut
s t y l e = { { h e i g h t: 4 0 , b a c k g ro un d C o l o r : ' a z u r e ' , f o n t S i z e : 2 0 } }
p l a c e h o ld e r = "Ty p e h e r e to t r a n s l a te ! "
o n C h a n g e Tex t = { ( tex t ) = > t h i s . s et S t a te ( { tex t } ) }
/>
< Tex t s t y l e = { { p a d d i ng : 1 0 0 , f o n t S iz e : 5 0 } } >
{ t h i s . s t a te . tex t . s p l i t ( ' ' ) . m a p ( ( wo r d ) = > w o r d & & ' 🍕 ' ) . j o i n (' ' ) }
< / Tex t > *
< / V i ew >
); } }
PROGRAM OUTPUT
REACT NATIVE LISTVIEW
ex p o r t d e f a u l t c l a s s M y L i s t C o mp o n e n t ex te n d s C o m p o n e n t {
c o n s t r uc tor () {
super();
c o n s t d s = n ew L i s t V i ew. D a t a S o urc e ( { row H a s C h a n g e d : ( r 1 , r 2 ) = > r 1 ! = = r 2 } ) ;
t h i s . s t a te = {
d a t a S o urc e : d s . c l o n e Wi t h Row s ( [' A n d r o id ' ,' i O S ' , ' J av a ' , ' P h p ' , ' H a d o o p ' ,
'Sap', 'Python','Ajax', 'C++',
' Ru by ' , ' R a i l s ' , ' . N et ' , ' Pe r l ' ] ) ,
};
}
render() {
r et u r n (
< L i s t V i ew
d a t a S o urc e = { t h i s . s t a te . d a t a S o urc e }
r e n d e r Row = {
(rowData) =>
< Tex t s t y l e = { { fo n t S i z e : 2 0 } } > { row Da t a }< / Tex t > }
/>
); } }
EXPLANATION
Props Description
disabled It is a Boolean property, if it is set to true then it cannot be toggled to
switch. Its default value is false.
trackColor It is used to customize the color for switch track.
ios_backgroundColo It set the custom background color in iOS. The background can be
r visible either when the switch value is disabled or when the switch is
false.
onValueChange It invoked when the switch value is changed.
testID It is used to locate this view in end-to-end tests.
thumbColor It colors the foreground switch grip. When it is set to iOS, the switch
grip will lose its drop shadow.
tintColor It sets the border color on iOS and background color on Android when
the switch is turned off. This property is deprecated; use trackColor at
the place of it.
value It is the value of switch. When it is set to true, it turns on. The default
value is false.
i m p o r t Re a c t , { C o m p o n e n t } f r o m ' r e a c t '
i m p o r t { S t y l e S h e e t , S w i t c h , V i e w, Te x t } f r o m ' r e a c t - n a t i v e '
e x p o r t d e f a u l t c l a s s S w i t c h E x a m p l e e x te n d s C o m p o n e n t {
s t a te = {
s w i t c h Va l u e : f a l s e
};
render() {
return (
<View style={styles.container }>
< Te x t s t y l e = { s t y l e s . te x t S t y l e } > S w i t c h E x a m p l e < / Te x t >
< Te x t s t y l e = { s t y l e s . te x t S t y l e } > { t h i s . s t a t e . s w i t c h Va l u e ? ' o n ' : ' o f f ' } < / Te x t >
<Switch
v a l u e = { t h i s . s t a t e . s w i t c h Va l u e }
o n Va l u e C h a n g e = { ( s w i t c h Va l u e ) = > t h i s . s e t S t a t e ( { s w i t c h Va l u e } ) } / >
</View>
);
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
a l i g n I t e m s : ' c e n te r ' ,
j u s t i f y C o n t e n t : ' c e n te r ' ,
},
te x t S t y l e : {
m a r g i n : 24 ,
fontSize: 25,
f o n t We i g h t : ' b o l d ' ,
te x t A l i g n : ' c e n te r ' ,
color: '#344953'
} })