Categories
React Native Answers

How to make a React Native app portrait only?

To make a React Native app portrait only, we change our app’s manifest.xml.

For instance, we write

<activity
  android:name=".MainActivity"
  android:configChanges="orientation"
  android:screenOrientation="portrait"
>
  ...
</activity>

to set android:configChanges to orientation to change the orientation of our app.

And we set android:screenOrientation to portrait to keep the app in portrait mode.

Categories
React Answers

How to set an item to have position absolute center with React Native?

To set an item to have position absolute center with React Native, we can set the position to absolute.

Then we set justifyContent and alignItems to center to center out item.

For instance, we write

<View
  style={{
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    justifyContent: "center",
    alignItems: "center",
  }}
>
  <Text>text here ...</Text>
</View>

to set the position, top, left, right and bottom values.

Then we center our View by setting justifyContent and alignItems to center.

Categories
React Native Answers

How to prevent rotation of screen with React Native?

To prevent rotation of screen with React Native, we can use the orientation field in your app.json file if our app is built with Expo.

For example, we write

{
  "expo": {
    "name": "My app",
    "slug": "my-app",
    "sdkVersion": "21.0.0",
    "privacy": "public",
    "orientation": "portrait"
  }
}

to set the orientation to portrait.

Categories
React Native Answers

How to add a progress bar slider with React Native?

To add a progress bar slider with React Native, we add the Slide component.

To use it, we write

import React, { Component } from 'react';

import { defaultString } from '../String/defaultStringValue';
import {
  View,
  Text,
  StyleSheet,
  Image,
  Slider,
  TouchableOpacity,
} from 'react-native';

function pad(n, width, z = 0) {
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

const minutesAndSeconds = (position) => ([
  pad(Math.floor(position / 60), 2),
  pad(position % 60, 2),
]);

const SeekBar = ({
  trackLength,
  currentPosition,
  onSeek,
  onSlidingStart,
}) => {
  const elapsed = minutesAndSeconds(currentPosition);
  const remaining = minutesAndSeconds(trackLength - currentPosition);
  return (
    <View style={styles.container}>
      <View style={{ flexDirection: 'row' }}>
        <Text style={[styles.text, { color: defaultString.darkColor }]}>
          {elapsed[0] + ":" + elapsed[1]}
        </Text>
        <View style={{ flex: 1 }} />
        <Text style={[styles.text, { width: 40, color: defaultString.darkColor }]}>
          {trackLength > 1 && "-" + remaining[0] + ":" + remaining[1]}
        </Text>
      </View>
      <Slider
        maximumValue={Math.max(trackLength, 1, currentPosition + 1)}
        onSlidingStart={onSlidingStart}
        onSlidingComplete={onSeek}
        value={currentPosition}
        minimumTrackTintColor={defaultString.darkColor}
        maximumTrackTintColor={defaultString.lightGrayColor}
        thumbStyle={styles.thumb}
        trackStyle={styles.track}
      />
    </View>
  );
};

export default SeekBar;

const styles = StyleSheet.create({
  slider: {
    marginTop: -12,
  },
  container: {
    paddingLeft: 16,
    paddingRight: 16,
    paddingTop: 16,
  },
  track: {
    height: 2,
    borderRadius: 1,
  },
  thumb: {
    width: 10,
    height: 10,
    borderRadius: 5,
    backgroundColor: defaultString.darkColor,
  },
  text: {
    color: 'rgba(255, 255, 255, 0.72)',
    fontSize: 12,
    textAlign: 'center',
  }
});


to add the Slider component into our view.

We set the maximumValue of the slide.

And we set various functions for the onSlidingStart and onSlidingComplete event handlers.

We also set the track color with minimumTrackTintColor and maximumTrackTintColor.

Categories
React Answers

How to get current language i18n-next React?

To get current language i18n-next React, we use the i18n.language.

For instance, we add

i18n.language

in our React app to get the current language.