How to set the orientation to portrait in firebase

2019-07-12 02:12发布

问题:

I am using firebase UI for authentication, In case of ioS the orientation is not an issue, In case of android if the screen orientation of phone is auto rotated, the firebase UI will also get rotated.

  • I have set the application orientation to portrait in manifest
  • I have also added code to make my activity portrait in my class.

Setting a custom UI to the Firebase UI with style

    <style name="FirebaseLoginTheme" parent="FirebaseUI">
    <item name="android:screenOrientation">portrait</item>
    <item name="android:windowContentOverlay">@null</item>
    </style>

does not worked. Is their any way to restrict it to portrait.

回答1:

add your a orientation in manifest file like this :

<activity
       android:name=".YourActivity"
       android:screenOrientation="portrait"
       android:theme="@style/FirebaseLoginTheme" />

Your Style

  <style name="FirebaseLoginTheme" parent="FirebaseUI">
   <item name="android:windowContentOverlay">@null</item>
  </style>


回答2:

I've solved this problem by setting the portrait mode programmatically in every activity. If you target Android 8+ you may get an error 'Only fullscreen activities can request orientation' from some com.firebase.ui.auth.ui activities and that's why I've used a try catch but your login activity will be still locked to the portrait mode.

Add this in your application class and remember to include it in your manifest's application tag (android:name=".MyApplication")

public class MyApplication extends Application{


public MyApplication() {

}


    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {

            try {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }catch (Exception e){
            }

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });
}

}