Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Image 1: The multiple screen sizes that a responsive design should cover
Percentage
Percentage is what we know from “normal” web development. The
problem with it, is that not all RN style properties accept it. In example:
margin , border-width , border-radius and many properties do not
accept it as value.
That being said, there is no option for a developer to make their app
responsive by coding everything in percentage…
Independent pixels
Independent pixels (dp) on the other hand, are not the classic screen
pixels (px) that we become accustomed to as web developers. They
mathematically connect to screen pixels and the screen’s scale factor
through the following equation:
px = dp * scaleFactor
For more info regarding screens and UI factors, you can have a look at
Android’s guide for pixel densities here, Android’s screen compatibility
overview and paintcodeapp’s guide for iPhone resolutions.
UI responsiveness
In order to create responsive UIs, you need to import and use these 2
methods called widthPercentageToDP and heightPercentageToDP . The
names are a bit big but they try to be indicative. That being said, both
methods accept a string like percentage ( '30%' ) as an argument and
return the percentage of the screen’s actual width or height respectively
in dp.
Let’s see this with an example. Samsung A5 2017 Android phone, has a
width of 360 dp (this is without taking the scale factor into account); so
if we code the following:
What happens here is that we create a simple screen with some text and
a view wrapper around it. We code the wrapper’s height to always be
the 70% of the screen’s width and the 80% of the screen’s height.
Finally we used smaller aliases for the methods’ big names. Check the
cross screen result below:
Image 2: Example of screen responsiveness across Android phones and tablets of di erent screen density
3. You can also provide decimal values to these 2 methods, i.e. font-
Let’s see how we can add orientation support to our previous example:
componentWillUnMount() {
rol();
}
render() {
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'gray',
alignItems: 'center',
justifyContent: 'center',
},
responsiveBox: {
width: wp('84.5%'),
height: hp('17%'),
borderWidth: 2,
borderColor: 'orange',
flexDirection: 'column',
justifyContent: 'space-around'
},
text: {
color: 'white'
}
});
return (
<View style={styles.container}>
<View style={styles.responsiveBox}>
<Text style={styles.text}>This box is always of
84.5% width and 17% height.</Text>
<Text style={styles.text}>Test it by running this
example repo in phones/
emulators with screens of various dimensions and
pixel per inch (ppi).</Text>
</View>
</View>
);
}
}
lifecycle method.
2. We added the orientation change listener remover in
componentWillUnmount lifecycle method. This is a crucial step, because
if we don’t do it, a new listener will be registered every time the
component re-mounts and can lead to performance issues or even
application crash.
3. styles object is created inside the render lifecycle method. That is
because every time the listener detects the orientation change it
triggers a re-render and thus the styles will be re-created.
marudy/react-native-responsive-screen
. . .
About me
About me
Hi, I’m Tasos, a software engineer that loves web and currently works a
lot with React Native and React. I take over project contracts and do
consulting. You can have a look over my portfolio here. If you want to
work with me or just say hi, contact me an email:
tasos.maroudas@codedlines.com