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();
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


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android