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

前端 未结 8 1470
情话喂你
情话喂你 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:05

    You can do this by:

    For Lanscape

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        //Do some stuff
    }
    

    For Portrait

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        //Do some stuff
    }
    

    Check: http://developer.android.com/reference/android/content/res/Configuration.html#orientation

    0 讨论(0)
  • 2020-12-07 23:07
    // Current orientation
    public boolean landscape = false;
    
    public boolean isLandscape(){
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;
        int height = displaymetrics.heightPixels;
    
        if(width<height){
            landscape = false;
        }
        else{
            landscape = true;
        }
    
        return landscape;
    
    }
    
    0 讨论(0)
提交回复
热议问题