Categories
React Native

React Native — Dimensions, Positioning, and Buttons

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

Width and Height

The width and height properties can be set to change the size of an item.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We set the width and height of the inner views in pixels to size the boxes.

The value can also be a percentage value or 'auto' .

Absolute and Relative Layout

In addition to flexbox layouts, we can also add absolute and relative layouts.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
    }}>
      <View style={{
        position: 'absolute',
        top: 10,
        width: 50,
        height: 50,
        backgroundColor: 'powderblue'
      }} />
      <View style={{
        position: 'relative',
        top: 80,
        left: 80,
        width: 50,
        height: 50,
        backgroundColor: 'skyblue'
      }} />
    </View>
  );
}

We set the position , top and left properties as we do with CSS.

Colors

We can set the color values with a color name, hex string, an rgb() value, an rgba() value, a hsl() value, or a hsla() value.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: '#8a2be2' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'rgb(255, 4, 65)' }} />
    </View>
  );
}

We set the backgroundColor of the view with various color values.

Handling Touches

We can add a Button component to add a component into our app.

For example, we can write:

import React from 'react';
import { View, Button, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <Button
        onPress={() => {
          alert('You tapped the button!');
        }}
        title="Press Me"
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

to add a Button component.

The onPress prop takes a function that’s run when we press the button.

The title prop has the title of the button.

Touchables

We can also add touchable components to add touchable elements that are more customizable than buttons.

For example, we can use the TouchableHighlight component to use it as a button.

To do that, we write:

import React from 'react';
import { View, StyleSheet, TouchableHighlight, Text } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <TouchableHighlight
        onPress={() => {
          alert('You tapped the button!');
        }}
      >
        <View style={styles.button}>
          <Text style={styles.buttonText}>TouchableHighlight</Text>
        </View>
      </TouchableHighlight>
    </View >
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    marginBottom: 30,
    width: 260,
    alignItems: 'center',
    backgroundColor: '#2196F3'
  },
  buttonText: {
    textAlign: 'center',
    padding: 20,
    color: 'white'
  }
});

to add a TouchableHighlight component with a View inside for the content.

Conclusion

We can set the width, height, and position of our React Native components.

Also, we can add buttons and touchable components to add touchable elements in our app.

Categories
React Native

React Native — Flexbox Alignment and Sizing

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

Align Content

We can use the alignContent property to set the alignment of the items in our React Native view.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'row',
      flexWrap: 'wrap',
      alignContent: 'flex-end'
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We set the alignContent to 'flex-end' with the flexDirection set to 'row' to put the inner views at the bottom of the screen.

The alignContent property is only applied when we set the flexWrap property.

The default value of 'flex-start' which puts the items at the top of the screen.

Other values include 'stretch' , 'center' , 'space-between' , and 'space-around' .

'stretch' stretches the items into the screen.

'center' centers the content.

'space-between' puts space between the item.

And 'space-around' makes the rows or columns evenly spaced.

Flex Wrap

The flexWrap property lets us set whether to wrap our rows or columns.

For instance, if we have:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'row',
      flexWrap: 'wrap',
      alignContent: 'flex-end'
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We set the flexWrap property to 'wrap' so that we wrap the rows.

Flex Basis, Grow, and Shrink

flexGrow describes how spaces within a container should be distributed among its children along the main axis.

It accepts any floating-point value bigger than or equal to 0.

flexShrink describes how to shrink children along the main axis in the case when the total size of the children overflows the size of the container.

It can also be bigger than or equal to 0.

flexBasis is an axis-independent way of providing the default size of an item along the main axis.

The flexBasis of a child is like setting the width of the child if its parent is a container with flexDiection set to row or setting the height of the child if it’s parent is a container with flexDiection set to column .

For example, we can use them by writing:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'row',
    }}>
      <View style={{ flexBasis: 200, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ flexGrow: 1, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ flexGrow: 3, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We use flexBasis to set the first view to 200px wide.

The rest are spread proportionally across the rest of the screen with the flexGrow properties.

This means 1/4 of the rest of the screen has the 2nd view.

And the rest of the screen has the 3rd view.

Conclusion

We can set flexbox properties to size and align content within React Native apps.

Categories
React Native

React Native — Flexbox Layout

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

Layout with Flexbox

We can use flexbox for layouts with React Native.

It works the same way as CSS.

Flex

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{ flex: 1, flexDirection: 'column' }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We create a column layout by setting flexDirection to 'column' on the outer view.

Then the inner views have their own dimensions set.

Layout Direction

The default direction for layouts is left to right.

It can also be set to right to left.

Justify Content

We can set the justifyContent property to spread the content the way we want.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'space-between',
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We have flexDirection set to 'column' and justifyContent set to 'space-between' .

Therefore, the inner views will be spread evenly in a column.

Align Items

We can set the alignItems property to align the items.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'center',
      alignItems: 'stretch',
    }}>
      <View style={{ height: 50, backgroundColor: 'powderblue' }} />
      <View style={{ height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

We set alignItems to 'stretch' , so the inner views will stretch across the screen.

Other values for alignItems can be 'flex-start' , 'center' , or 'flex-end' .

Align Self

The alignSelf property is also available with React Native.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'center',
    }}>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue', alignSelf: 'flex-end' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'skyblue' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'steelblue' }} />
    </View>
  );
}

to add the alignSelf property to our first inner view.

It’s set to 'flex-end' so it’ll be put on the right side.

Conclusion

We can use many flexbox properties to create layouts with React Native.

Categories
React Native

React Native — View Styles

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

View Style Props

We can set styles for views.

For example, we can write:

import React from 'react';
import { View, StyleSheet } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.top} />
      <View style={styles.middle} />
      <View style={styles.bottom} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "space-between",
    backgroundColor: "#fff",
    padding: 20,
    margin: 10,
  },
  top: {
    flex: 0.3,
    backgroundColor: "grey",
    borderWidth: 5,
    borderTopLeftRadius: 20,
    borderTopRightRadius: 20,
  },
  middle: {
    flex: 0.3,
    backgroundColor: "beige",
    borderWidth: 5,
  },
  bottom: {
    flex: 0.3,
    backgroundColor: "pink",
    borderWidth: 5,
    borderBottomLeftRadius: 20,
    borderBottomRightRadius: 20,
  },
});

We use common CSS properties to set the style of the views.

flex is the proportion of the screen that the view takes.

justifyContent sets the alignment for the content.

backgroundColor sets the background color.

borderWidth sets the border width of the view.

padding sets the padding of the view.

margin sets the margin of the view.

PressEvent Object Type

The PressEvent object has various properties.

This type of object is set as the event object when a button is pressed.

For example, if we have:

import React from 'react';
import { View, StyleSheet, Text, SafeAreaView, Button } from "react-native";

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <View>
        <Button
          title="Press me"
          onPress={(ev) => console.log(ev)}
        />
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    marginHorizontal: 16,
  }
});

We log the event object for button presses, which should be a PressEvent .

It has many properties, including:

{
    changedTouches: [PressEvent],
    identifier: 1,
    locationX: 8,
    locationY: 4.5,
    pageX: 24,
    pageY: 49.5,
    target: 1127,
    timestamp: 85131876.58868201,
    touches: []
}

identifier is the ID of the button press.

locationX is the x-coordinate of the button press.

locationY is the y-coordinate of the button press.

pageX is the touch origin’s x-coordinate on the screen.

pageY is the touch origin’s y-coordinate on the screen.

target is the ID of the element receiving the press event.

timestamp is the timestamp when the PressEvent occurred in milliseconds.

Height and Width

We can set the height and width of components.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View>
      <View style={{ width: 50, height: 50, backgroundColor: 'powderblue' }} />
    </View>
  );
}

We set the width and height properties to set the width and height of the View .

Also, we can use flexbox to set the dimensions.

For example, we can write:

import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <View style={{ flex: 1, backgroundColor: 'powderblue' }} />
    </View>
  );
}

We set the flex property of the views to fill the outer View with the inner View .

Conclusion

We can set various styles for views.

Also, we can get data from button press events.

Categories
React Native

React Native — Layouts and Fonts

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

Layout Props

We can control the layout of our app with some props.

For example, we can write:

import React from 'react';
import { View } from "react-native";

const randomHexColor = () => {
  return '#000000'.replace(/0/g, () => {
    return (~~(Math.random() * 16)).toString(16);
  });
};

const Square = () => {
  const sqStyle = {
    width: 50,
    height: 50,
    backgroundColor: randomHexColor(),
  };
  return <View style={sqStyle} />;
};

export default function App() {
  return (
    <View style={{
      flexDirection: 'row',
      justifyContent: 'flex-start',
      alignItems: 'flex-start'
    }}>
      <Square />
      <Square />
      <Square />
    </View>
  );
}

We added some squares with the Square component.

Then we set the style of the View with some flexbox properties.

We can use all the flexbox properties like flexDirection , justifyContent , alignItems , etc.

And we can set them to the value that we want.

Shadow Props

We can set a shadow on a container by setting the shadowOffset property.

For example, we can write:

import React from 'react';
import { View, StyleSheet } from "react-native";
const shadowOffsetWidth = 20;
const shadowOffsetHeight = 20;
const shadowOpacity = 0.5;
const shadowRadius = 10;

export default function App() {
  return (
    <View style={styles.container}>
      <View style={[
        styles.square,
        {
          shadowOffset: {
            width: shadowOffsetWidth,
            height: -shadowOffsetHeight
          },
          shadowOpacity,
          shadowRadius
        }
      ]}>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "space-around",
    backgroundColor: "#ecf0f1",
    padding: 8
  },
  square: {
    alignSelf: "center",
    backgroundColor: "white",
    borderRadius: 4,
    height: 150,
    shadowColor: "black",
    width: 150
  },
  controls: {
    paddingHorizontal: 12
  }
});

We set the width and height of the shadow offset within the shadowOffset property.

Also, we set the shadow’s opacity with the shadowOpacity property.

And we set the shadow’s radius with the shadowRadius property.

Text Style Props

We can set styles for text.

For example, we can write:

import React from 'react';
import { View, StyleSheet, Text } from "react-native";
const fontStyles = ["normal", "italic"];
const fontVariants = [
  undefined,
  "small-caps",
  "oldstyle-nums",
  "lining-nums",
  "tabular-nums",
  "proportional-nums"
];
const fontWeights = [
  "normal",
  "bold",
  "100",
  "200",
  "300",
  "400",
  "500",
  "600",
  "700",
  "800",
  "900"
];
const textAlignments = ["auto", "left", "right", "center", "justify"];
const textDecorationLines = [
  "none",
  "underline",
  "line-through",
  "underline line-through"
];
const textDecorationStyles = ["solid", "double", "dotted", "dashed"];
const textTransformations = ["none", "uppercase", "lowercase", "capitalize"];
const textAlignmentsVertical = ["auto", "top", "bottom", "center"];
const writingDirections = ["auto", "ltr", "rtl"];

export default function App() {
  return (
    <View style={styles.container}>
      <Text
        style={[
          styles.paragraph,
          {
            fontSize: 20,
            fontStyle: fontStyles[0],
            fontWeight: fontWeights[0],
            lineHeight: 200,
            textAlign: textAlignments[0],
            textDecorationLine: textDecorationLines[0],
            textTransform: textTransformations[0],
            textAlignVertical: textAlignmentsVertical[0],
            fontVariant: fontVariants[0],
            letterSpacing: 5,
            includeFontPadding: true,
            textDecorationStyle: textDecorationStyles[0],
            writingDirection: writingDirections[0],
            textShadowOffset: {
              height: 10,
              width: 10
            },
            textShadowRadius: 5
          }
        ]}
      >
        Lorem Ipsum
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "space-around",
    backgroundColor: "#ecf0f1",
    padding: 8
  },
  paragraph: {
    color: "black",
    textDecorationColor: "yellow",
    textShadowColor: "red",
    textShadowRadius: 1,
    margin: 24
  }
});

to set all the styles for our text.

fontSize sets the font size.

fontStyle sets the font style.

lineHeight sets the line-height.

textAlign sets the text alignment.

textDecorationLine sets the text style.

textTransform lets us transform the text.

fontVariant sets the font-variant.

letterSpacing sets the letter spacing.

writingDirection sets the direction of the text.

textShadowOffset sets the shadow’s position.

textShadowRadius sets the radius of the shadow.

Conclusion

We can set many styles for fonts and layouts.