Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design screens in react native compatible for all devices

I am trying to design this design react-native. This is what I have coded for this but this is not what I want. This works on one screen only, if I change screen size then things are not working.

This looks like absolute layout. What changes should I make to make it in such a way so that it will work on all screen sizes.

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from "react";
import {
  AppRegistry,
  Image,
  View,
  Text,
  Button,
  StyleSheet
} from "react-native";

class SplashScreen extends Component {
  render() {
    console.disableYellowBox = true;
    return (
      <View style={styles.container}>
        <Image
          source={require("./img/talk_people.png")}
          style={{ width: 300, height: 300 }}
        />
        <Text style={{ fontSize: 22, textAlign: "center", marginTop: 30 }}>
          Never forget to stay in touch with the people that matter to you.
        </Text>
        <View style={{ marginTop: 60, width: 240 }}>
          <Button title="CONTINUE" color="#FE434C" />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: "#FFFFFF",
    margin: 50,
    alignItems: "center",
    flex: 1,
    flexDirection: "column"
  }
});

AppRegistry.registerComponent("Scheduled", () => SplashScreen);

Expected State:

enter image description here

Current State:

Nexus 4 - 768x1280

enter image description here

Nexus 6P - 1440x2560 enter image description here

like image 820
N Sharma Avatar asked Apr 14 '17 16:04

N Sharma


People also ask

How do I make multiple monitors in React Native?

Show activity on this post. import React from 'react'; import {TextInput, StyleSheet, Text, View } from 'react-native'; import {Button} from 'react-native-elements'; import { Navigation } from 'react-native-navigation'; import Signup from "./Signup"; export default function App() { return ( <View style={styles.

How does React Native deal with a variety of screen sizes?

React Native uses Dots Per Inch (DPI) to measure the sizing of the User Interface (UI) and anything displayed on the UI. This type of measurement allows an application to look uniform across various screen sizes and pixel densities.


1 Answers

The quick answer is to use flex within your outer container, such that you have, say:

<View style={{flex: 1}}>
    <View style={{flex: 2}}>
       <.../>//Image
    </View>
    <View style={{flex: 1}}>
       <.../>//Text
    </View>
    <View style={{flex: 1}}>
       <.../>//Button
    </View>
</View>

which will divide the container into quarters and give the top half of the screen to the image and the other two quarters to the text and button; you can use padding and margins with these as you like, and use any ratio you want.

What further needs to be considered, though, is screen pixel density, which can really wreak havoc with display sizes. I've found it handy to have an outside

import React from 'react';
import { PixelRatio } from 'react-native';
let pixelRatio = PixelRatio.get();

export const normalize = (size) => {
  switch (true){
    case (pixelRatio < 1.4):
        return size * 0.8;
        break;
    case (pixelRatio < 2.4):
        return size * 1.15;
        break;
    case (pixelRatio < 3.4):
        return size * 1.35;
        break;
    default:
        return size * 1.5;
  }
}

export const normalizeFont = (size) => {
  if (pixelRatio < 1.4){
    return Math.sqrt((height*height)+(width*width))*(size/175);
  }
  return Math.sqrt((height*height)+(width*width))*(size/100);
}

module which I use as

import { normalize, normalizeFont } from '../config/pixelRatio';
const {width, height} = require('Dimensions').get('window');

...and for an image, say:

<Image source={ require('../images/my_image.png') } style={ { width: normalize(height*.2), height: normalize(height*.2) } } />

and a font:

button_text: {
    fontSize: normalizeFont(configs.LETTER_SIZE * .7),
    color: '#ffffff'
},

Hope this helps!

Edit: The module above has worked for me for devices I've deployed to, but it should be expanded to allow for pixelRatio values of from 1 to 4 with some decimal (e.g. 1.5) values in there, too. There's a good chart at this link that I'm working from to try to finish this out, but so far what has worked best is as I've posted above.

like image 85
kwishnu Avatar answered Sep 21 '22 19:09

kwishnu