Reputation: 477
I was wondering if anyone knows a simple way to get the height and width of an android screen size using c# in Xamarin. I need to get the screen size so that I can use it in one of my adapter classes. I have tried using the display and the IWindowManager but without success. Any help or a point in the right direction would be much appreciated. Thanks in advance!!
Upvotes: 6
Views: 13169
Reputation: 3963
For real device screen size, use Xamarin.Essentials.
The difference between this and the accepted answer is that this will give your hardware accurate numbers, but that one will give you the size with the status and navigation bar reduced.
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var realHeightInPx = mainDisplayInfo.Height;
Upvotes: 1
Reputation: 1142
var metrics = Resources.DisplayMetrics;
height = metrics.HeightPixels;
width = metrics.WidthPixels;
Upvotes: 7
Reputation: 477
The answer that worked best for me was using
var metrics = Resources.DisplayMetrics;
Thank you all for your help!
Upvotes: 18
Reputation: 508
Here a code snippet to get some info about the screen:
var metrics = new DisplayMetrics();
var windowManager = this.GetSystemService(Context.WindowService) as IWindowManager;
windowManager.DefaultDisplay.GetMetrics(metrics);
var height = metrics.HeightPixels;
var width = metrics.WidthPixels;
var xdpi = metrics.Xdpi;
var ydpi = metrics.Ydpi;
var density = metrics.Density;
The only thing that I'm not sure is the casing to the IWindowManager if can be done like this or maybe you have to do something much more specific. Hope it will help you.
Upvotes: 5