Hey I need to get the width of the screen in my application. The application will run on 2.1 and upwards. I have set it up like the one below. The method is deprecated and i should proabably use getSize or a other way. But the question is: Will this work on android versions like 3.0+ and 4.0+, or will it make the app crash. I have used a deprecated method in a thread before and it made the app crash on ice cream devices. Will the method below work ?
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
EDIT:
I have tried the getSize but i dont get it to work:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.
I am not sure but this may work:
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
} else {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
}
I don't know whether these deprecated methods will work on Android 3 and 4. The best way to tell is to test on an emulator.
But, the safest method here for max compatibility will be to try one method using reflection, and fall back to the other. Essentially, you could make your own version of getSize()
that can't fail. I can't test this atm, but it might look like this:
void overrideGetSize(Display display, Point outSize) {
try {
// test for new method to trigger exception
Class pointClass = Class.forName("android.graphics.Point");
Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });
// no exception, so new method is available, just use it
newGetSize.invoke(display, outSize);
} catch(NoSuchMethodException ex) {
// new method is not available, use the old ones
outSize.x = display.getWidth();
outSize.y = display.getHeight();
}
}
Then of course just call it with something like
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
overrideGetSize(display, size);
I've extended Steve's helpful code so that Eclipse doesn't give any warnings or errors, and I've also restructured it slightly. Since the Point class has been present since API level 1 I didn't see much benefit in creating it through reflection.
final static String mTAG = "MYTAG";
// Cope with deprecated getWidth() and getHeight() methods
Point getSize(Display xiDisplay)
{
Point outSize = new Point();
boolean sizeFound = false;
try
{
// Test if the new getSize() method is available
Method newGetSize =
Display.class.getMethod("getSize", new Class[] { Point.class });
// No exception, so the new method is available
Log.d(mTAG, "Use getSize to find screen size");
newGetSize.invoke(xiDisplay, outSize);
sizeFound = true;
Log.d(mTAG, "Screen size is " + outSize.x + " x " + outSize.y);
}
catch (NoSuchMethodException ex)
{
// This is the failure I expect when the deprecated APIs are not available
Log.d(mTAG, "getSize not available - NoSuchMethodException");
}
catch (InvocationTargetException e)
{
Log.w(mTAG, "getSize not available - InvocationTargetException");
}
catch (IllegalArgumentException e)
{
Log.w(mTAG, "getSize not available - IllegalArgumentException");
}
catch (IllegalAccessException e)
{
Log.w(mTAG, "getSize not available - IllegalAccessException");
}
if (!sizeFound)
{
Log.i(mTAG, "Used deprecated methods as getSize not available");
outSize = new Point(xiDisplay.getWidth(), xiDisplay.getHeight());
}
return outSize;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With