Question

I have an issue with detecting screen size on device which does not have hardware buttons.

For example in portrait working screen size 800x1232, but in landscape 1280x752.

In my app buttons located at the border and I need exact size of the screen while interface changing orientation.

How to get future screen size in willRotateToInterfaceOrientation:duration: ?

How to detect if device has hardware menu buttons or not? And will be perfect to get the size of menu bar, if buttons not hardware.

Thanks

Was it helpful?

Solution

I've used Android SDK to resolve this problem. You can look how use java with apportable here.

This is my java code

public Point screenSize(Activity activity) {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();

    int API_LEVEL =  android.os.Build.VERSION.SDK_INT;
    if (API_LEVEL >= 17) {
        disp.getRealMetrics(displayMetrics);
    } else {
        disp.getMetrics(displayMetrics);
    }

    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    return new Point(width, height); //String.format("%d,%d,%d,%d", portraitNavBarHeight, landscapeNavBarHeight, width, height);
}

public Point navigationBarHeights(Activity activity) {

    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int resourceIdLandscape = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
    int portraitNavBarHeight = 0;
    int landscapeNavBarHeight = 0;

    boolean hasPermanentMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();
    if (resourceId > 0 && !hasPermanentMenuKey) {
        portraitNavBarHeight = resources.getDimensionPixelSize(resourceId);
    }

    if (resourceIdLandscape > 0 && !hasPermanentMenuKey) {
        landscapeNavBarHeight = resources.getDimensionPixelSize(resourceIdLandscape);
    }

    return new Point(portraitNavBarHeight, landscapeNavBarHeight); //String.format("%d,%d,%d,%d", portraitNavBarHeight, landscapeNavBarHeight, width, height);
}

and Objective-C part

+ (void)initializeJava
{
[super initializeJava]; 

    // here your initialize code
    ...

    [KeyboardBridge registerInstanceMethod:@"screenSize"
                                  selector:@selector(screenSize:)
                               returnValue:[AndroidPoint className]
                                 arguments:[AndroidActivity className], nil];

    [KeyboardBridge registerInstanceMethod:@"navigationBarHeights"
                                  selector:@selector(navigationBarHeights:)
                               returnValue:[AndroidPoint className]
                                 arguments:[AndroidActivity className], nil];

}

- (CGRect)displayMetrics {
    AndroidPoint *screenSize = [self screenSize:[AndroidActivity currentActivity]];
    AndroidPoint *navigationBarHeights = [self navigationBarHeights:[AndroidActivity currentActivity]];

    return CGRectMake(navigationBarHeights.x, navigationBarHeights.y, screenSize.x, screenSize.y);
}

and final result

CGRect rect = [[[KeyboardBridge alloc] init] displayMetrics];

float portraitWidth;
float portraitHeight;
if(rect.size.width > rect.size.height) {
    portraitHeight = rect.size.width;
    portraitWidth = rect.size.height;
} else {
    portraitHeight = rect.size.height;
    portraitWidth = rect.size.width;
}

landscapeSize.width =  portraitHeight;
landscapeSize.height = portraitWidth - rect.origin.y;

portraitSize.width = portraitWidth;
portraitSize.height = portraitHeight - rect.origin.x;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top