1

In HarmonyOS, I am trying to create a commonDialog and add a stackLayout of screen size to it. This layout helps me to move a component (for example., an image) to any location on the screen. To do so I need the width and height of the entire screen, how do I get these parameters?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108

1 Answers1

1

Welcome to the community!

If you are looking for screen width and height return types values as int, I am providing you the solution I use. We compute screen width and height using HarmonyOS APIs as below:

import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import java.util.Optional;

public class Utils {

  private Utils() { }

    public static int getScreenWidth(Context context) {
        DisplayManager displayManager = DisplayManager.getInstance();
        Optional<Display> optDisplay = displayManager.getDefaultDisplay(context);
        Point point = new Point(0, 0);
        if (optDisplay.isPresent()) {
            Display display = optDisplay.get();
            display.getSize(point);
        }
        return (int) point.position[0];
    }

    public static int getScreenHeight(Context context) {
        DisplayManager displayManager = DisplayManager.getInstance();
        Optional<Display> optDisplay = displayManager.getDefaultDisplay(context);
        Point point = new Point(0, 0);
        if (optDisplay.isPresent()) {
            Display display = optDisplay.get();
            display.getSize(point);
        }
        return (int) point.position[1];
    } 
}
Kanak Sony
  • 1,570
  • 1
  • 21
  • 37