Search code examples
androidscreen-resolutionscreen-sizesamsung-s8

Restricting an app's screen size based on a phone's resolution


I am developing an application in android studio which is displayed correctly on 1080x1920 phones(and lower) but for phones like mine(s8) the app gets messed up and i can't find a way to fix the app's ui for every screen size. So, i was wondering if there is a way to restrict the app's screen size to 1080x1920(with let's say black bars at the top & bottom of the screen) even if a phone's dimensions are bigger than that(this is automatically done in games that are not compatible with s8). Do you guys know a way of achieving this effect?


Solution

  • It's possible, you can set your parent View to exact size given the aspect ratio you want (1080x1920 scales down to 9x16). For this you'd have to programmatically call the View (or create it in code) and set width and height in pixels with LayoutParams:

    yourView.setLayoutParams(new LayoutParams(width, height));
    

    for the weight and height you'd want the actual screen dimensions, which you can get from the WindowManager:

    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point point = new Point();
    display.getSize(point);
    int x = point.x;
    int y = point.y;
    

    Here you get the x and y, which hold your actual screen size (in pixels). Then, you decide on which one should be reduced (in order to maintain 9x16 aspect ratio) and set your parent View's LayoutParams as mentioned above.

    Finally, your set your content View as your parent View:

    setContentView(yourView);
    

    That's about it. Hope that helps.

    EDIT: Since you've mentioned games, I can tell you that there (if your use Android SDK) you have your SurfaceView as a parent View, the procedure is same.