Fetching screen height, finding screen orientation

Here I am going to explain some simple but useful tips in Android.

Fetch Screen Height and width of our Android Phone


To fetch the screen height and width of our Android phone, the code is
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;

To find the orientation of our Android Phone


To find out the orientation, that is whether it is in landscape, portrait or in any another orientation, the code is,

int orientation = getResources().getConfiguration().orientation; 
This will give the current orientation. To check whether it is in landscape or portrait,
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
         // do something
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
         // do something else
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android