How to check in a certain situation if the smartphone is horizontal?
How to check in a certain situation if the smartphone is horizontal?
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 about this way of checking, they say it's not reliable, 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";
}
}
I saw your comment and you know that using the measures, this way that Carlos Bridi posted and very good, but if you using measures 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;
}
}
/ p>
public boolean isLandscape()
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (metrics.widthPixels>metrics.heightPixels);
}