Find out if Android device is portrait or landscape for normal usage?

前端 未结 8 1545
情话喂你
情话喂你 2020-12-07 22:05

Is there anyway to find out if a device is portrait or landscape by default? In that I mean how you normally use the device.

Most phones have a portrait screen for

8条回答
  •  [愿得一人]
    2020-12-07 23:02

    I am not sure if this will help but this is a quick example I wrote that will show you how to have a listener...which will allow you to figure out which orientation you are in.

    ...textviewOrientation = (TextView)findViewById(R.id.textView1);
    
        myOrientationEventListener = new OrientationEventListener(this, 
                  SensorManager.SENSOR_DELAY_NORMAL){
    
            @Override
            public void onOrientationChanged(int arg0) {
    
             textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
             myOrientationEventListener.enable();
    
    
                 if ((arg0 < 100) || (arg0 > 280)){
    
                     setRequestedOrientation(
                         ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
                 } else {
    
                     setRequestedOrientation(
                         ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
                 }
    
    
            }
    
        };...
    
    
    ...@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        myOrientationEventListener.disable();
    }...
    

提交回复
热议问题