Check orientation on Android phone

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

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

23条回答
  •  一个人的身影
    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.
      

提交回复
热议问题