Lazarus

Programming => Operating Systems => Android => Topic started by: bmcsoft on June 24, 2021, 05:18:18 pm

Title: How to get screen width and height?
Post by: bmcsoft 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.
Title: Re: How to get screen width and height?
Post by: Bart on June 24, 2021, 06:53:57 pm
Screen.Width and Screen.Heigth don't work on Android?

Bart
Title: Re: How to get screen width and height?
Post by: loaded 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.
Title: Re: How to get screen width and height?
Post by: Bart 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
Title: Re: How to get screen width and height?
Post by: loaded 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.
Title: Re: How to get screen width and height?
Post by: Bart 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
Title: Re: How to get screen width and height?
Post by: jmpessoa 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"
Title: Re: How to get screen width and height?
Post by: bmcsoft 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.
Title: Re: How to get screen width and height?
Post by: bmcsoft 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.
Title: Re: How to get screen width and height?
Post by: loaded 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.  
Title: Re: How to get screen width and height?
Post by: Mongkey 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
Title: Re: How to get screen width and height?
Post by: jmpessoa 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***
Title: Re: How to get screen width and height?
Post by: Mongkey 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
TinyPortal © 2005-2018