Need Help in Changing Screen Orientation in Android

后端 未结 3 451
無奈伤痛
無奈伤痛 2021-01-24 16:03

I want to change the screen orientation from portrait to landscape and vice versa when the user rotates his Android mobile phone clockwise/anticlockwise. Can anyone help me how

相关标签:
3条回答
  • 2021-01-24 16:15

    Hai, I got Solution with the help of Georgy Gobozov, But that solution has to be refined as shown below to work it properly,

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
    
         if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE))
         {
             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
         }
         else if(newConfig.equals(Configuration.ORIENTATION_PORTRAIT))
         {
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
         }
        } 
    
    0 讨论(0)
  • 2021-01-24 16:26
     public class Accelerometer extends Activity 
        implements AccelerometerListener{
    
        public void onShake(float force) {
             Toast.makeText(this, "Phone shaked : " + force, 1000).show();
        }
     }
    

    You can refer to below link

    http://blog.androgames.net/85/android-accelerometer-tutorial/

    0 讨论(0)
  • 2021-01-24 16:30

    Just add to your AndroidManifest in activity

    android:configChanges="keyboardHidden|orientation"
    

    Then you should override this method in your activity

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.myLayout);
    }
    

    And use search, this question is very popular.

    0 讨论(0)
提交回复
热议问题