Way to get display resolution

Post your feature requets for new triggers, conditions, actions and other improvements.

Moderator: Martin

User avatar
acerzw
Posts: 39
Joined: 10 May 2015 13:38

Way to get display resolution

Post by acerzw » 23 Sep 2017 19:50

IT would be useful to have an action or function to return the screen X and y resolution in pixels

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Way to get display resolution

Post by Desmanto » 24 Sep 2017 03:37

I would just use terminal command to retrieve the resolution. It should be available on most android, especially Lollipop 5.0 above.
Action : Execute Command
Command : wm size

just leave everything default. If the resolution is Full HD, The stdout (output of command), should be
Physical size: 1080x1920

From here, we wanna extract out the width and the height. Use script to parse the resolution. There are other ways to do it, but it is easier to use regex.

Code: Select all

res = findAll(stdout, "Physical size: (\\d+)x(\\d+)", true);
width = res[0][1];
height = res[0][2];
(\\d+) will match one or more digit and capture it as group (the bracket capture it). The result of capture is stored at index [0][1] and [0][2]. Use the {width} and {height} as needed.

You can also use command wm density to get the density (dpi).
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
acerzw
Posts: 39
Joined: 10 May 2015 13:38

Re: Way to get display resolution

Post by acerzw » 24 Sep 2017 07:53

Thanks that's great.

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Way to get display resolution

Post by Bingwu » 24 Sep 2017 12:19

Hello Desmanto!

I also wanted to try your suggestion! :-)

I get the following error message (stderr) in the Execute Command (wm size):

Code: Select all

Error type 2
android.util.AndroidException: Can't connect to window manager; is the system running?
	at com.android.commands.wm.Wm.onRun(Wm.java:81)
	at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
	at com.android.commands.wm.Wm.main(Wm.java:46)
	at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
	at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:301)
Am I doing something wrong :? or can it be that it does not work on Android 7?

Tested with:
Samsung Galaxy S7 edge, Android 7.0, Stock-ROM, non rooted
Asus Nexus 7 (2. Gen.), Android 7.1.2, LineageOS, rooted

Regards
Peter

User avatar
acerzw
Posts: 39
Joined: 10 May 2015 13:38

Re: Way to get display resolution

Post by acerzw » 24 Sep 2017 12:48

You know I also got the same on my Samsung t285 Tab A 7" (rooted lineage OS 7.12)

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Way to get display resolution

Post by bogdyro » 24 Sep 2017 13:51

Hi,the command works using root.
(execute root command)

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Way to get display resolution

Post by bogdyro » 24 Sep 2017 14:14

You can do it without root by using Java.
Check the attached flow.
Maybe Martin could implement it as a separate action.
http://automagic4android.com/flow.php?i ... b61d92766e

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Way to get display resolution

Post by Desmanto » 24 Sep 2017 16:24

@Bingwu : Don't know what's wrong. Try to install terminal emulator and type the command directly to see if it works at there. But seems using java is more general; as the function to retrieve it must be available on every android out there (if not, it will be less useful then)

@bogdyro : How can you get until that? :shock: That is nesting so many parameter. I don't know we have to nest it so many times to get something like this. That explains why I mostly failed to use the java function. Wish I have know more about java. This should have work better that using execute command.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Way to get display resolution

Post by Bingwu » 24 Sep 2017 19:06

Hello bogdyro and Desmanto!

Thanks for your help! :-)

@bogdyro: Your flow (action) I have tested on my Asus Nexus 7. It unfortunately gives me an incorrect value for the display height (1824 instead of 1920). The display width is correct (1200).
It is also not a problem, it would have been a practical feature!

Regards
Peter

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Way to get display resolution

Post by Desmanto » 25 Sep 2017 02:34

That is correct. If you rotate it to landscape, the height will be correct, 1920, while the width will become less (probably 1104). 96 pixel reduction is from on screen navigation button (recent, home, back).
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Locked