Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation: StackNavigator transition for Android

I am using this library https://reactnavigation.org/docs/intro/ to build android by react-native. I can make the navigation happens on android device but how I can make the screen slide in from the right and fade in from the left. It seems that this behaviour happens on iOS device but not in Android. Is there any animation configuration for android app?

Please see below animation. This is recorded in iOS.

enter image description here

like image 241
Joey Yi Zhao Avatar asked Feb 26 '17 06:02

Joey Yi Zhao


People also ask

Is react navigation using native activity for Android?

React Navigation is written in JavaScript and does not directly use the native navigation APIs on iOS and Android.

How do I set initial screen in react navigation?

Use initialRouteName props (The name of the route to render on first load of the navigator) if you want a specific screen, otherwise the first stack screen will be rendered by default.

What is Createstacknavigator?

Provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in from the bottom on Android.

Can I use react navigation with Expo?

React Navigation is the most popular navigation library in the React Native ecosystem and the best choice for most apps. It is maintained by the Expo team and supports Android, iOS, and the web. This video demonstrates using React Navigation on iOS, Android, and web.


2 Answers

Starting from : "@react-navigation/native": "^5.5.1",

import {createStackNavigator, TransitionPresets} from '@react-navigation/stack';


const TransitionScreenOptions = {
  ...TransitionPresets.SlideFromRightIOS, // This is where the transition happens
};

const CreditStack = createStackNavigator();

function CreditStackScreen() {
  return (
    <CreditStack.Navigator screenOptions={TransitionScreenOptions}> // Don't forget the screen options
      <CreditStack.Screen
        name="Credit"
        component={HomeScreen}
        options={headerWithLogo}
      />
      <HomeStack.Screen
        name="WorkerDetails"
        component={WorkerDetails}
        options={headerWithLogoAndBackBtn}
      />
    </CreditStack.Navigator>
  );
}

You can watch this video to understand more: https://www.youtube.com/watch?v=PvjV96CNPqM&ab_channel=UnsureProgrammer

like image 94
Mohammed Sabri Avatar answered Sep 21 '22 08:09

Mohammed Sabri


You should use transitionConfig to override default screen transitions as written on this page.

Unfortunately there is no example provided how that function works but you can find some examples in this file: \react-navigation\lib\views\CardStackStyleInterpolator.js

So your code should look like this:

 const navigator = StackNavigator(scenes, {
transitionConfig: () => ({
    screenInterpolator: sceneProps => {
        const { layout, position, scene } = sceneProps;
        const { index } = scene;

        const translateX = position.interpolate({
            inputRange: [index - 1, index, index + 1],
            outputRange: [layout.initWidth, 0, 0]
        });

        const opacity = position.interpolate({
            inputRange: [
                index - 1,
                index - 0.99,
                index,
                index + 0.99,
                index + 1
            ],
            outputRange: [0, 1, 1, 0.3, 0]
        });

        return { opacity, transform: [{ translateX }] };
    }
})
});
like image 20
robocub Avatar answered Sep 21 '22 08:09

robocub