Check orientation on Android phone

前端 未结 23 1600
庸人自扰
庸人自扰 2020-11-22 06:26

How can I check if the Android phone is in Landscape or Portrait?

相关标签:
23条回答
  • 2020-11-22 07:03

    I think this solution easy one

    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
      user_todat_latout = true;
    } else {
      user_todat_latout = false;
    }
    
    0 讨论(0)
  • 2020-11-22 07:03

    Simple and easy :)

    1. Make 2 xml layouts ( i.e Portrait and Landscape )
    2. At java file, write:

      private int intOrientation;
      

      at onCreate method and before setContentView write:

      intOrientation = getResources().getConfiguration().orientation;
      if (intOrientation == Configuration.ORIENTATION_PORTRAIT)
          setContentView(R.layout.activity_main);
      else
          setContentView(R.layout.layout_land);   // I tested it and it works fine.
      
    0 讨论(0)
  • 2020-11-22 07:05

    The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration object:

    getResources().getConfiguration().orientation;
    

    You can check for orientation by looking at its value:

    int orientation = getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // In landscape
    } else {
        // In portrait
    }
    

    More information can be found in the Android Developer.

    0 讨论(0)
  • 2020-11-22 07:06

    there are many ways to do this , this piece of code works for me

     if (this.getWindow().getWindowManager().getDefaultDisplay()
                    .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                 // portrait mode
    } else if (this.getWindow().getWindowManager().getDefaultDisplay()
                    .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                          // landscape
            }
    
    0 讨论(0)
  • 2020-11-22 07:09

    I think this code may work after orientation change has take effect

    Display getOrient = getWindowManager().getDefaultDisplay();
    
    int orientation = getOrient.getOrientation();
    

    override Activity.onConfigurationChanged(Configuration newConfig) function and use newConfig,orientation if you want to get notified about the new orientation before calling setContentView.

    0 讨论(0)
  • 2020-11-22 07:10
    int ot = getResources().getConfiguration().orientation;
    switch(ot)
            {
    
            case  Configuration.ORIENTATION_LANDSCAPE:
    
                Log.d("my orient" ,"ORIENTATION_LANDSCAPE");
            break;
            case Configuration.ORIENTATION_PORTRAIT:
                Log.d("my orient" ,"ORIENTATION_PORTRAIT");
                break;
    
            case Configuration.ORIENTATION_SQUARE:
                Log.d("my orient" ,"ORIENTATION_SQUARE");
                break;
            case Configuration.ORIENTATION_UNDEFINED:
                Log.d("my orient" ,"ORIENTATION_UNDEFINED");
                break;
                default:
                Log.d("my orient", "default val");
                break;
            }
    
    0 讨论(0)
提交回复
热议问题