Search code examples
androidquickaction

QuickAction by Lorensius


I'm attempting to removed the deprecated code getWidth(); from this code before going to production. I've tried a few of the ways mentioned here at stack for other classes but cannot seem to get it to work for this. Anyone know i can get the with here?

public class QuickAction extends PopupWindows implements OnDismissListener {
...
public void show (View anchor) {
    int screenWidth     = mWindowManager.getDefaultDisplay().getWidth();
...
}

Solution

  • I am not sure what have you tried so far, but you could use this:

     final int version = android.os.Build.VERSION.SDK_INT;
     Display display = mWindowManager.getDefaultDisplay();
     int screenWidth;
     if (version >= 13) {
         Point size = new Point();
         display.getSize(size);
         screenWidth = size.x;
     }
     else {
         screenWidth = display.getWidth();
     }
    

    Like javaDoc says method display.getWidth() was deprecated in API level 13 so you should use display.getSize(Point) instead.

    In case you don't like "magic" numbers, instead of 13 you can use android.os.Build.VERSION_CODES.HONEYCOMB_MR2.