How to retrieve the screen size of the device ?


#1

Hi,

How to retrieve the screen size of the device as soon as the application is loaded?

Thanks and regards, Jithesh


#2

Retrieve it from JavaScript or Uno code?

In Uno you do:

int2 screenSize = Uno.Application.Current.Window.ClientSize;

We do not provide any JS APIs for this yet, but you can easily implement a NativeModule for this

For example:

using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;

public class AppModule : NativeModule
{
    public AppModule()
    {
        AddMember(new NativeFunction("GetClientSize", (NativeCallback)GetClientSize));
    }

    static object GetClientSize(Context c, object[] args)
    {
        var size = Uno.Application.Current.Window.ClientSize;
        return c.NewArray(size.X, size.Y);
    }
}


<AppModule ux:Global="AppModule" />
<JavaScript>
    var AppModule = require("AppModule");
    var w = AppModule.GetClientSize()[0];
    var h = AppModule.GetClientSize()[1];
    console.log("width: " + w + ", height: " + h);
</JavaScript>


#3

Uno.Exception: Window ClientSize not available on mobile

‘Uno.Application.Current.Window.ClientSize’ is works on windows preview. but I have runtime fatal error on android.

So, I tried call widthPixels and heightPixels from getRootActivity().getResources().getSystem().getDisplayMetrics()

Android native code can get the resolution of the screen, but fuse is not compatible because it uses point coordinates.

How can I get through this problem?


#4

Same Problem.
Is there any news?


#5

In UX/JS you can use the Placed event to retrieve the size of any element:

 <Panel Placed="{placed}">

And in JS:

  function placed(args) {

Where you have args.width and args.height.

https://www.fusetools.com/docs/fuse/elements/element/placed

However, this should almost never be needed. I’m interested to know what your use cases are, there are probably a better way of doing it.


#6

Well one case for example is that you want to call an api that returns images and you want to submit the screen resolution of the phone, so the api can return smaller images for smaller phones, which saves traffic.
That’s at least what i’m working on and also the reason why i came across this thread.