Reputation: 9309
I want to get the screen size and I'm using a device that has API level 10. I'm using Display
to get the width and height, but I'm getting a message in Eclipse that this is deprecated. A better and newer way is to use DisplayMetrics
for apps that have a newer API, but there is no support for DisplayMetrics
in API 10. How should I be doing to handle this issue?
Old(deprecated)
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
New
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
Upvotes: 3
Views: 1298
Reputation: 1615
Just check the API level like this:
int screenWidth = 0;
int screenHeight = 0;
if(android.os.Build.VERSION.SDK_INT < 10) {
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
} else {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
}
But DisplayMetrics
was added in API level 1
. So actually you only need the else-clause.
Upvotes: 7
Reputation: 643
In my application I use getResources().getDisplayMetrics()
directly on my activity object. It exactly gives me the screen size. It works fine as it is available from API level 1.
Upvotes: 0