kimv
kimv

Reputation: 1579

Calculate Android screen size

I have this piece of code :

    Point point = new Point();
    getWindowManager().getDefaultDisplay().getSize(point);
    setScreenSize(point);

This returns the total height of the display in pixels. However, this includes the notification bar and, on some devices, the bottom bar that contains the android-specific buttons (back, home and that other one).

My question is a two-parter.

How can I find out the height of the notification bar? How can I find if the device has those buttons on the screen, and if it does, what is the height of that bar?

Upvotes: 1

Views: 115

Answers (3)

kamal vaid
kamal vaid

Reputation: 862

You can try these.I recently used below code in one of my projects. It works

Display display = getWindowManager().getDefaultDisplay();
String displayName = display.getName();  // minSdkVersion=17+
Log.i(TAG, "displayName  = " + displayName);

// display size in pixels

Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.i(TAG, "width        = " + width);
Log.i(TAG, "height       = " + height);

// get in (pixels or dpi)

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
int widthPixels = metrics.widthPixels;
int densityDpi = metrics.densityDpi;

// this one is deprecated

int screenHeight = display.getHeight();
int screenWidth = display.getWidth();
Log.i(TAG, "screenHeight = " + screenHeight);
Log.i(TAG, "screenWidth  = " + screenWidth);

Upvotes: 2

Hiren Patel
Hiren Patel

Reputation: 52810

You can do this way:

Display mDisplay = getWindowManager().getDefaultDisplay();
Point mPoint = new Point();
mDisplay.getSize(mPoint);
int width = mPoint.x;
int height = mPoint.y;

Hope this will help you.

Upvotes: 0

RheeBee
RheeBee

Reputation: 195

You can find a lot of the default Android specifications such as StatusBar (Metrics & Keylines) and NavBar (Structure) in the Android Material Design Specification...

I believe that status bar is 24dp and NavBar is 48dp as standard.

You can get the width and height in DP by using:

DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();

float screenWidthDP = displayMetrics.widthPixels / displayMetrics.density;
float screenHeightDP = displayMetrics.heightPixels / displayMetrics.density;

Material Design Specification

Upvotes: 0

Related Questions