React-native-orientation: Orientation not changing on Android 6.0.1

Created on 9 Sep 2016  Â·  13Comments  Â·  Source: yamill/react-native-orientation

I'm testing on a Nexus 5 with Android 6.0.1 installed and I have a button which, when pressed, is supposed to toggle orientation (landscape/portrait and so on). The button, however, is not working: if the phone is on portrait, it stays locked on portrait and doesn't change to landscape, even if I unlock all orientations before locking again. I'm still a bit unsure if it's a RN issue rather than this, but it's always nice to ask.

Thanks for your concern.

P.S.: this approach works fine on iOS.

Most helpful comment

It works for me on Android 6.0.1.

No mystery. I am an idiot. I was missing my onConfigurationChanged from MainActivity.java.

Sorry for the noise. The answers are all in the README

https://github.com/yamill/react-native-orientation/blob/master/README.md

package com.acme.myapp;

import android.content.Intent; 
import android.content.res.Configuration; 

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
  @Override
  protected String getMainComponentName() {
    return "MyApp";
  }

  // react-native-orientation
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Intent intent = new Intent("onConfigurationChanged");
    intent.putExtra("newConfig", newConfig);
    this.sendBroadcast(intent);
  }
}

All 13 comments

Also, if I lock orientation, it doesn't return me the right orientation (i.e. locked to portrait, then flipped device to landscape, instead of returning PORTRAIT it returns LANDSCAPE).

  • Orientation is broken on Android. I do not know when it broke.
  • No clues are seen in adb.
  • It is still working for me on iOS with the following:
    "react": "^15.3.1",
    "react-native": "^0.33.0",
    "react-native-orientation": "git+https://github.com/yamill/react-native-orientation",

On Android I never see the console.log placed inside _orientationDidChange()

I have checked my Android installation versus the react-native-orientation README several times.

I have run the following before each build:
(cd android && ./gradlew clean)

My Android target:

RN 0.33
Nexus 7
Android 6.0.1

Does anyone have it working with latest RN 0.33 on Android ??

@esutton I suggest you try xiongzhend's solution : https://github.com/yamill/react-native-orientation/issues/96. It worked for me with [email protected].

@yspychala Thank you very much for the tip! I will give it a try https://github.com/yamill/react-native-orientation/issues/96#issuecomment-242745023

@yspychala Is solution https://github.com/yamill/react-native-orientation/issues/96#issuecomment-242745023 the same as changing the apps package.json to point directly to github?

"react-native-orientation": "git+https://github.com/yamill/react-native-orientation",

This is what I have had all along. It seems like it should be the same but since it does not work for me, I will try solution https://github.com/yamill/react-native-orientation/issues/96#issuecomment-242745023

@esutton Yes, the solution is to point to github (at least for now). Then I advice you linking manually the dependence. The react-native-link didn't work well for me and some required changes were missing.

I have solved this issue by adding in my MainActivity a listener that listens for accelerometer oscilations and broadcasts the orientation change if the phone is inclined at a given angle. But this is bulky and messy, and I should never have to mess with MainActivity while developing. I'll post the solution later on.

My solution was to have MainActivity implement SensorEventListener and fire the orientation events manually by listening to the accelerometer. I think, however, that this wastes a great deal of battery.

I tried follow the step @xiongzhend mentioned, but still can't get it work on Android, reading undefined is not an object(evulating 'Orientation.lockToLandscape')

Can you use this on Android now?

OK, It worked mysteriously.

I believe that previous failure was due to ↓ when running react-native run-android

:app:prepareAPPReactNativeMPAndroidChartUnspecifiedLibrary UP-TO-DATE
:app:prepareAPPReactNativeOrientationUnspecifiedLibrary FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preparePower51APPReactNativeOrientationUnspecifiedLibrary'.
> Could not expand ZIP 'C:\Users\linonetwo\Desktop\AppAndroid\node_modules\react-native-orientation\android\build\outputs\aar\react-native-orientation-release.aar'.

I just ignore that error, then rerun react-native run-android then it worked.

It works for me on Android 6.0.1.

No mystery. I am an idiot. I was missing my onConfigurationChanged from MainActivity.java.

Sorry for the noise. The answers are all in the README

https://github.com/yamill/react-native-orientation/blob/master/README.md

package com.acme.myapp;

import android.content.Intent; 
import android.content.res.Configuration; 

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
  @Override
  protected String getMainComponentName() {
    return "MyApp";
  }

  // react-native-orientation
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Intent intent = new Intent("onConfigurationChanged");
    intent.putExtra("newConfig", newConfig);
    this.sendBroadcast(intent);
  }
}

Hey - I'm installing directly from Github, but Orientation is still locked to Portrait on Android devices. Help??

@aschwizzy710 you no longer need to install from GitHub. Check the updated README file. Just run npm install react-native-orientation --save and follow the rest of the installation guidelines. Cheers

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yamill picture yamill  Â·  8Comments

MrEdinLaw picture MrEdinLaw  Â·  3Comments

sahas- picture sahas-  Â·  7Comments

shaikhussian picture shaikhussian  Â·  8Comments

AmmiWang picture AmmiWang  Â·  3Comments