Recent

Author Topic: How to get screen width and height?  (Read 6867 times)

bmcsoft

  • New Member
  • *
  • Posts: 18
How to get screen width and height?
« on: June 24, 2021, 05:18:18 pm »
Hello, we need to obtain screen width and height, depending on the rotation of the device.

I've found such code:
Code: Pascal  [Select][+][-]
  1. Display display = getWindowManager().getDefaultDisplay();
  2. int width = display.getWidth();
  3. int height = display.getHeight();
and newest variant:
Code: Pascal  [Select][+][-]
  1. DisplayMetrics metrics = new DisplayMetrics();
  2. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  3. int height = metrics.heightPixels;
  4. int width = metrics.widthPixels;
Anybody help to translate it in Lazarus code and post result code in this topic.

Bart

  • Hero Member
  • *****
  • Posts: 4342
    • Bart en Mariska's Webstek
Re: How to get screen width and height?
« Reply #1 on: June 24, 2021, 06:53:57 pm »
Screen.Width and Screen.Heigth don't work on Android?

Bart

loaded

  • Full Member
  • ***
  • Posts: 228
Re: How to get screen width and height?
« Reply #2 on: June 24, 2021, 07:25:47 pm »
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  2. begin
  3.   showmessage(inttostr(GetRealScreenWidth())+'x'+inttostr(GetRealScreenHeight()));
  4. end;
  5.  


Screen.Width and Screen.Heigth don't work on Android?

Bart

No it doesn't work.
Installing the spelling, google translate is used :)

Bart

  • Hero Member
  • *****
  • Posts: 4342
    • Bart en Mariska's Webstek
Re: How to get screen width and height?
« Reply #3 on: June 24, 2021, 07:41:11 pm »
Screen.Width and Screen.Heigth don't work on Android?

Bart

No it doesn't work.

Sounds like a bug to me?

Bart

loaded

  • Full Member
  • ***
  • Posts: 228
Re: How to get screen width and height?
« Reply #4 on: June 24, 2021, 08:04:51 pm »
It's not a bug, ;D
LAMW does not use LCL. It cannot be used in Screen.Width because it is defined in the forms unit (LCL).
If I'm wrong, someone who knows better can clarify.
Installing the spelling, google translate is used :)

Bart

  • Hero Member
  • *****
  • Posts: 4342
    • Bart en Mariska's Webstek
Re: How to get screen width and height?
« Reply #5 on: June 24, 2021, 08:07:47 pm »
OK, then I simply misunderstood.

(It will come as no surprise to you that I don't do android programming.)

Bart

jmpessoa

  • Hero Member
  • *****
  • Posts: 1871
Re: How to get screen width and height?
« Reply #6 on: June 24, 2021, 11:04:36 pm »
Quote
I don't do android programming....

But you can!!

About rotation,  you can start with demo "AppPanelRotateDemo1"

and "AppCameraDemo"
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

bmcsoft

  • New Member
  • *
  • Posts: 18
Re: How to get screen width and height?
« Reply #7 on: June 25, 2021, 07:18:35 am »
Quote
I don't do android programming....

But you can!!
All we can. But if this elementary operation has not received a solution, then what can we talk about.

bmcsoft

  • New Member
  • *
  • Posts: 18
Re: How to get screen width and height?
« Reply #8 on: June 25, 2021, 07:20:11 am »
It's not a bug, ;D
LAMW does not use LCL. It cannot be used in Screen.Width because it is defined in the forms unit (LCL).
Yes, I need to get screen resolution in LAMW.

loaded

  • Full Member
  • ***
  • Posts: 228
Re: How to get screen width and height?
« Reply #9 on: June 25, 2021, 08:18:41 am »
You must have overlooked it, The method you should use for Resolution;
Code: Pascal  [Select][+][-]
  1.   GetRealScreenWidth();
  2.   GetRealScreenHeight();
  3.  
Installing the spelling, google translate is used :)

Mongkey

  • Full Member
  • ***
  • Posts: 120
Re: How to get screen width and height?
« Reply #10 on: June 25, 2021, 04:25:07 pm »
The LAMW samples should cover all minimum android apps should work, and it is enough for general app development, for more advance things you should read and practice a lot, that why instant noodle are more cheaper a lot than real man made ramen  :D

jmpessoa

  • Hero Member
  • *****
  • Posts: 1871
Re: How to get screen width and height?
« Reply #11 on: June 25, 2021, 10:20:08 pm »
Quote
But if this elementary operation has not received a solution, then what can we talk about.....

You can get help from Lazarus smart editor.... using:

Self.

To access this jForm   functions/procedures.....

Code: Pascal  [Select][+][-]
  1.     procedure SetScreenOrientationStyle(_orientation: TScreenStyle);
  2.     function  GetScreenOrientationStyle(): TScreenStyle;
  3.  
  4.     function  GetScreenSize(): string;
  5.     function  GetScreenDensity(): string; overload;
  6.     function  GetScreenDpi(): integer;
  7.     function  GetScreenDensity(strDensity: string): integer; overload;
  8.     procedure SetDensityAssets( _value : TDensityAssets ); // by ADiV
  9.  
  10.  
  11.     function  GetScreenWidth(): integer;      // by ADiV
  12.     function  GetScreenHeight(): integer;     // by ADiV
  13.     function  GetRealScreenWidth(): integer;  // by ADiV
  14.     function  GetRealScreenHeight(): integer; // by ADiV
  15.  

But, remember that Android can handle these issues... If you have a good Layout design
and the perfect image pixel in  the project folders   ..../res/drawable***
« Last Edit: June 25, 2021, 10:29:56 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Mongkey

  • Full Member
  • ***
  • Posts: 120
Re: How to get screen width and height?
« Reply #12 on: June 26, 2021, 05:01:33 am »
Quote
I don't do android programming....

But you can!!
All we can. But if this elementary operation has not received a solution, then what can we talk about.

I see your square game panels are 8x8, why not you placing 1/8 parent to height and width, let say you need 2 block height of your screen height for setting or seeing match result, you can do by 1/10 relatif to parent for height.  :), So you not need allocate you time concerning pixel, screen resolution, let LAMW handle for u. Practice n talking to the code is the key  :D
« Last Edit: June 26, 2021, 05:04:02 am by Mongkey »

 

TinyPortal © 2005-2018