Check smartphone horizontally

Asked

Viewed 64 times

0

How to check in a given situation if the smartphone is horizontal?

2 answers

2

You can use the following strategy:

getResources().getConfiguration().orientation

And the result will be according to the documentation that is here. However, I’ve seen some people complaining this way to check, say it is unreliable, and you can use an android service:

public String getRotation(Context context){
    final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
           switch (rotation) {
            case Surface.ROTATION_0:
                return "portrait";
            case Surface.ROTATION_90:
                return "landscape";
            case Surface.ROTATION_180:
                return "reverse portrait";
            default:
                return "reverse landscape";
            }
        }
  • Thanks for the tip Carlos, I managed to do as follows, in the event onResize form, checking if the width is greater than height.

1

I saw your comment and I know using the measures , this way that Carlos Bridi posted is very good , but if you see that using measures ai you can use this code below:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.activity_main);

        // get the display metrics
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        boolean isLandscape = width > height;
    }
}

ai vc check if the width is greater than height the device is in Landscape mode that would be horizontal , in this case I used a Boolean but there are several ways to do , I think the best would be to make a method for it , type :

public boolean isLandscape()
{
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (metrics.widthPixels>metrics.heightPixels);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.