Getting screen size android with xamarin

11,083

Solution 1

The answer that worked best for me was using

var metrics = Resources.DisplayMetrics;

Thank you all for your help!

Solution 2

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.

Solution 3

var metrics = Resources.DisplayMetrics;

        height = metrics.HeightPixels;
        width = metrics.WidthPixels;
Share:
11,083
Aaron B
Author by

Aaron B

I'm a mobile application developer specializing in cross platform applications and python. I'm based out of Sheridan Wyoming but is willing to travel and/or relocate anywhere for the right opportunity. Over the last few years, I have focused on education, teaching students and professionals about technology and new technology-based products with the goal of helping them reach their project and professional objectives. Most recently, he taught a computer repair course at a local college where students were excited about the program, and eager to learn more about computer science, including object-oriented programming. In 2019, while attending an internship at Dakota State University in South Dakota, I instructed interns on the Python programming language to assist in the scope of their research projects. Since it was an internship centered around security testing, he led a “hack the box group” that met on the weekends. Besides teaching others about technology, I have been working on many professional projects. During my time at DSU, I've built an enterprise network program for system performance monitoring and cryptojacking network detection. I've also worked on a cross platform mobile app wrote in Flutter, which was designed for Android/IOS and interacts with 3rd party API’s, Stripe MSP, Firebase Cloud-Firestore and Cloud-Functions, Geo Name, Google Places/Maps API, and also does multi factor auth via phone. In addition to new development, I have helped clients with new designs for their current applications as well.

Updated on June 21, 2022

Comments

  • Aaron B
    Aaron B almost 2 years

    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!!