I need to apply different styles on orientation change between two landscape positions i.e landscapeRight to landscapeLeft or vice versa in React Native? I know how to detect between portrait and landscape, just need a listener between two landscape positions in REACT NATIVE!
Thanks!
You would have to use an external dependency to manage this. Something like react-native-orientation-locker can give you what you want. This option requires you to be using a full react-native project as it will require you to link native code, so it will not work in Expo. You can read more about it here. Below are the basic usage instructions from the repo.
You can install it in the following way:
npm install react-native-orientation-locker --save
react-native link react-native-orientation-locker
There are a few extra steps that you need to do after you have linked it.
Configuration
iOS
Add the following to your project's AppDelegate.m:
+#import "Orientation.h"
#implementation AppDelegate
// ...
+- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
+ return [Orientation getOrientation];
+}
#end
Android
Add following to android/app/src/main/AndroidManifest.xml
<activity
....
+ android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
....
</activity>
Implement onConfigurationChanged method (in MainActivity.java)
// ...
+import android.content.Intent;
+import android.content.res.Configuration;
public class MainActivity extends ReactActivity {
+ #Override
+ public void onConfigurationChanged(Configuration newConfig) {
+ super.onConfigurationChanged(newConfig);
+ Intent intent = new Intent("onConfigurationChanged");
+ intent.putExtra("newConfig", newConfig);
+ this.sendBroadcast(intent);
+ }
// ......
}
Getting the orientation:
Then you can set a listener where you want to access the orientation, usually you add this in the componentDidMount and remove move it in componentWillUnmount.
Here is some example code that you can use to set it up.
import Orientation from 'react-native-orientation-locker';
_onOrientationDidChange = (orientation) => {
if (orientation === 'LANDSCAPE-LEFT') {
//do something with landscape left layout
}
if (orientation === 'LANDSCAPE-RIGHT') {
//do something with landscape right layout
}
};
componentWillMount() {
//The getOrientation method is async. It happens sometimes that
//you need the orientation at the moment the js starts running on device.
//getInitialOrientation returns directly because its a constant set at the
//beginning of the js code.
var initial = Orientation.getInitialOrientation();
if (initial === 'PORTRAIT') {
//do stuff
} else {
//do other stuff
}
},
componentDidMount() {
Orientation.getAutoRotateState((rotationLock) => this.setState({rotationLock}));
//this allows to check if the system autolock is enabled or not.
Orientation.lockToPortrait(); //this will lock the view to Portrait
//Orientation.lockToLandscapeLeft(); //this will lock the view to Landscape
//Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations
//get current UI orientation
/*
Orientation.getOrientation((orientation)=> {
console.log("Current UI Orientation: ", orientation);
});
//get current device orientation
Orientation.getDeviceOrientation((deviceOrientation)=> {
console.log("Current Device Orientation: ", deviceOrientation);
});
*/
Orientation.addOrientationListener(this._onOrientationDidChange);
},
componentWillUnmount () {
Orientation.removeOrientationListener(this._onOrientationDidChange);
}
This will mean that in your _onOrientationDidChange function it will receive on of the following:
'PORTRAIT'
'LANDSCAPE-LEFT' camera left home button right
'LANDSCAPE-RIGHT' camera right home button left
'PORTRAIT-UPSIDEDOWN'
'UNKNOWN'
Related
The opening view of App is blank with a Back Button. However, the intended view will appear after complete rotation of physical device (before touching anything) or by tapping the Back Button on the displayed empty view.
This is a weird bug. After that rotation (to landscape and back to protrait) everything is peachy-keen-o.
I am aware that adding: .navigationViewStyle(StackNavigationViewStyle()) to the NavigationView will sorta resolve the problem. But this solution is a poor fix for my App's needs.
Is there a command to intentionally rotate device in code to resolve this? Or some other fix to get that first view to appear without counting on the user to rotate the device?
I've tried various solutions with no luck.
Below is a quick setup of the problem.
struct FirstView: View {
#Binding var viewNum: Int?
var body: some View {
VStack {
Text("View One")
.padding(20)
Button {
viewNum = 2
} label: {
Text("Go to 2nd view")
}
}
.onAppear {
viewNum = 1
}
}
}
struct MainView: View {
#State var selection: Int? = 1
var body: some View {
// VStack {
NavigationView {
List {
NavigationLink("First View", tag: 1, selection: $selection){ FirstView(viewNum: $selection)}
NavigationLink("Second View", tag: 2, selection: $selection){
Text("View 2").padding(20)
Button {
selection = 1
} label: {
Text("Go to first view")
}
}
}
}
// .navigationViewStyle(StackNavigationViewStyle())
//}
}
}
How would I get the coordinates of my onTouchEnd event. So I touch or move figure anywhere within the display and I can retrieve the X, Y positioning of where it happened. onResponderRelease is not triggered in a onTouchEnd on Android,
I've included example implementations for all of the gesture response event handlers, but have commented out most of them on my View to just provide the basic functionality: subscribing to all touch and move events
pseudo code would look like this :
<View
style={this.props.style}
ref={this._saveRef}
onStartShouldSetResponder={(event) => {
return this.handleDoubleTap({nativeEvent:
event.nativeEvent});
}}
onResponderGrant={this._onTouchStart}
onResponderMove={this._onTouchMove}
onResponderRelease={this._onTouchEnd}
onResponderTerminate={this._onTouchEnd} // When
onResponderRelease can't call by some reason
>
{this.props.children}
</View>
Responder Event Handler Methods :
if (this.isDoubleTap) {
return false;
}
this.context.setScroll && this.context.setScroll(false);
const currentTouchTimeStamp = Date.now();
this._prevTouchInfo = {
prevTouchX: nativeEvent.pageX,
prevTouchY: nativeEvent.pageY,
prevTouchTimeStamp: currentTouchTimeStamp
};
this.props.onStart(nativeEvent);
};
_onTouchMove = ({nativeEvent}) => {
if (nativeEvent.touches.length <= 1) {
if (this.isDoubleTap) {
return false;
}
const self = this;
const gesture = {
x0: this._prevTouchInfo.prevTouchX,
x1: nativeEvent.pageX,
y0: this._prevTouchInfo.prevTouchY,
y1: nativeEvent.pageY,
dx: nativeEvent.pageX - this._prevTouchInfo.prevTouchX,
dy: nativeEvent.pageY - this._prevTouchInfo.prevTouchY
};
InteractionManager.runAfterInteractions(function () {
self.props.onMove(nativeEvent, gesture);
});
}
};
_onTouchEnd = ({nativeEvent}) => {
nativeEvent.touches.length === 0 && this.context.setScroll && this.context.setScroll(true);
this.props.onEnd(nativeEvent);
};
}
I am Developing an Application in React native.Actually when i am touch on Particular Position On view, getting the Corresponding x and y co-ordinates. App UI would look like this:
If this is still not enough functionality for android (eg. if you need multi-touch info), refer to PanResponde
i am Developing one Application, Actually my App is showing landscape and Portrait in Tabs/iPads. But in Tablets(iPad's working fine) when i was check the Orientation functionality working fine until unlock the Device.When i was lock the screen on Particular mode like (Portrait/Landscape) After that turned the Device showing before Orientation. Not Update the present Orientation.
I followed this link :https://github.com/yamill/react-native-orientation
this is my code:
componentWillMount(){
this.getOrientationtype()
}
getOrientationtype(){
//alert("Hello")
if(Platform.OS == 'ios'){ // to Identifying Android or iOS
if(aspectRatio>1.6){ // Code for Iphone
// alert("phone")
Orientation.lockToPortrait()
}
else{
Orientation.getOrientation((err, initial) => {
if(initial != 'UNKNOWN'){
this.setState({
orientation:initial
})
}
else{
this.setState({
orientation:'PORTRAIT'
})
}
});
}
}
else{
if(DeviceInfo.isTablet()){
// alert("android tab")
Orientation.getOrientation((err, initial) => {
if(initial != 'UNKNOWN'){
this.setState({
orientation:initial
})
}
else{
this.setState({
orientation:'PORTRAIT'
})
}
});
}
else{
Orientation.lockToPortrait()
}
}
}
Please find Out this Solution ....I am Using this link enter link description here
1.You should use Orientation.addOrientationListener for listen to the Orientation Events.
2.As see from the source code of OrientationModule.java, this library just call unregisterReceiver in onHostPause, so you can't receive onConfigurationChanged event after lock the screen.One way is to edit the onHostResume inside OrientationModule.java to meet what you want.
#Override
public void onHostResume() {
final Activity activity = getCurrentActivity();
if (activity == null) {
FLog.e(ReactConstants.TAG, "no activity to register receiver");
return;
}
activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
//add below code to onHostResume function
//send broadcast onResume
final int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;
Configuration newConfig = new Configuration();
newConfig.orientation = orientationInt;
Intent intent = new Intent("onConfigurationChanged");
intent.putExtra("newConfig", newConfig);
activity.sendBroadcast(intent);
}
the whole code can be found here OrientationModule.java
I want to border margin of of screen S on phone and tablet to be different. There are variants for tablet landscape and portrait mode.
How to create different margin dimension for the variants on phone, tablet portrait, tablet landscape ?
For those curious how to do on Android , we just create some resource files at the right folder :
values for default
values-sw600dp for tablet default
values-sw600dp-land for tablet landscape
The other answers have already addressed the screen detection task. However, there is still the issue of detecting if the code is running on a Tablet device. You can detect that using the react-native-device-info package, in particular its isTablet method. So, as an example, in your component:
constructor(){
super();
this.state = {orientation: 'UNKNOWN'}
this._onOrientationChanged = this._onOrientationChanged.bind(this);
}
_onOrientationChanged(orientation){
this._setState({orientation})
}
componentDidMount(){
Orientation.addOrientationListener(this._onOrientationChanged);
}
componentWillUnmount(){
Orientation.removeOrientationListener(this._orientationDidChange);
}
render(){
let layoutStyles;
if(DeviceInfo.isTablet()){
layoutStyles = this.state.orientation == 'LANDSCAPE' ? landscapeTabletStyle : portraitTabletLandscape; // Basic example, this might get more complex if you account for UNKNOWN or PORTRAITUPSIDEDOWN
}else{
layoutStyles = this.state.orientation == 'LANDSCAPE' ? landscapeStyle : portraitLandscape;
}
render(){
<View style={[styles.container, layoutStyles]} // And so on...
}
}
Note that the state holds the UNKNOWN value on the beginning. Have a look at the getInitialOrientation() of the package function. I am intentionally leaving that bit out because it simply reads a property that is set when the JS code loads, and I am not sure if that satisfies your usecase (i.e. this is not your first screen). What I usually like to do is store the rotation value in a redux store (where I initialize the orientation value to that of getInitialOrientation() and then subscribe only once to the orientation listener).
I think this library will be helpful for you: https://github.com/yamill/react-native-orientation
You can do something like that with it:
Orientation.getOrientation((err,orientation)=> {
console.log("Current Device Orientation: ", orientation);
if(orientation === 'LANDSCAPE') {
//do stuff
} else {
//do other stuff
}
});
// Extract from the root element in our app's index.js
class App extends Component {
_onLayout = event => this.props.appLayout(event.nativeEvent.layout);
render() {
return (
<View onLayout={this._onLayout}>
{/* Subviews... */}
</View>
);
}
}
export const SET_ORIENTATION = 'deviceStatus/SET_ORIENTATION';
export function appLayout(event: {width:number, height:number}):StoreAction {
const { width, height } = event;
const orientation = (width > height) ? 'LANDSCAPE' : 'PORTRAIT';
return { type: SET_ORIENTATION, payload: orientation };
}
Code Copied from Here
I have the following code in my app class:
switchMainView: function (newView, config) {
if (this.currentView != false) {
Ext.Viewport.remove(this.currentView);
}
this.currentView = Ext.create(newView, config);
Ext.Viewport.animateActiveItem(this.currentView, { type: 'slide', direction: 'right' })
},
Which can be called from any controller. However, it is not showing the animation. Any ideas what is wrong with it?
According to sencha guys :
AnimateActiveItem(activeItem,animation ) will
Animates to the supplied activeItem with a specified animation. Currently this only works with a Card layout. This passed animation will override any default animations on the container, for a single card switch. The animation will be destroyed when complete.
viewport: {
layout: {
type: 'card'
}
}
add above code in app.js
This is because the "this.currentView" view isn't part of the viewport yet. Use this line before the animateActiveItem line:
Ext.Viewport.add(this.currentView);
This should make it work.