How to detect orientation change in layout in Android?

后端 未结 10 2145
挽巷
挽巷 2020-11-22 09:36

I just implemented a orientation change feature - e.g. when the layout changes from portrait to landscape (or vice versa). How can I detect when the orientation change event

相关标签:
10条回答
  • 2020-11-22 09:43

    Override activity's method onConfigurationChanged

    0 讨论(0)
  • 2020-11-22 09:45

    Just wanted to show you a way to save all your Bundle after onConfigurationChanged:

    Create new Bundle just after your class:

    public class MainActivity extends Activity {
        Bundle newBundy = new Bundle();
    

    Next, after "protected void onCreate" add this:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                onSaveInstanceState(newBundy);
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                onSaveInstanceState(newBundy);
            }
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle("newBundy", newBundy);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        savedInstanceState.getBundle("newBundy");
    }
    

    If you add this all your crated classes into MainActivity will be saved.

    But the best way is to add this in your AndroidManifest:

    <activity name= ".MainActivity" android:configChanges="orientation|screenSize"/>
    
    0 讨论(0)
  • 2020-11-22 09:46

    If accepted answer doesn't work for you, make sure you didn't define in manifest file:

    android:screenOrientation="portrait"
    

    Which is my case.

    0 讨论(0)
  • 2020-11-22 09:48

    Use the onConfigurationChanged method of Activity. See the following code:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
    

    You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:

    <activity android:name=".MyActivity"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name">
    

    NOTE: with Android 3.2 (API level 13) or higher, the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher, you must declare android:configChanges="orientation|screenSize" for API level 13 or higher.

    Hope this will help you... :)

    0 讨论(0)
  • 2020-11-22 09:54

    use this method

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            Toast.makeText(getActivity(),"PORTRAIT",Toast.LENGTH_LONG).show();
           //add your code what you want to do when screen on PORTRAIT MODE
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            Toast.makeText(getActivity(),"LANDSCAPE",Toast.LENGTH_LONG).show();
            //add your code what you want to do when screen on LANDSCAPE MODE
       }
    }
    

    And Do not forget to Add this in your Androidmainfest.xml

    android:configChanges="orientation|screenSize"
    

    like this

    <activity android:name=".MainActivity"
              android:theme="@style/Theme.AppCompat.NoActionBar"
              android:configChanges="orientation|screenSize">
    
        </activity>
    
    0 讨论(0)
  • 2020-11-22 09:56

    Create an instance of OrientationEventListener class and enable it.

    OrientationEventListener mOrientationEventListener = new OrientationEventListener(YourActivity.this) {
        @Override
        public void onOrientationChanged(int orientation) {
            Log.d(TAG,"orientation = " + orientation);
        }
    };
    
    if (mOrientationEventListener.canDetectOrientation())
        mOrientationEventListener.enable();
    
    0 讨论(0)
提交回复
热议问题