Roland Mai
Roland Mai

Reputation: 31077

Monodroid screen dimensions

I'm trying to reference WindowManger so I can get the default screen's dimensions but I can't seem to find reference to it. (this is API 8, froyo 2.2). I even tried:

dynamic wm = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.getDefaultDisplay();

But got an error indicating that Object doesn't respond to getDefaultDisplay.

Also I tried:

var wm = (IWindowManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.DefaultDisplay;

But I get an invalid cast exception.

Saw this post, but I can't resolve WindowManager. Any ideas what's going on here?

Upvotes: 2

Views: 961

Answers (3)

Jason
Jason

Reputation: 2609

using Android.Runtime; //for JavaCast

var windowManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService).JavaCast<IWindowManager>();

Upvotes: 0

Jake
Jake

Reputation: 356

Another simpler way is

Display display = this.WindowManager.DefaultDisplay;

You can get the Width and Height properties from the display object.

Upvotes: 1

jonp
jonp

Reputation: 13600

WindowManager is the IWindowManager interface. Instead of the cast, try using the .JavaCast<T>() extension method:

var wm = context.GetSystemService(Android.Content.Context.WindowService)
    .JavaCast<IWindowManager>();
var d  = wm.DefaultDisplay;

You can also check out the AccelerometerPlay sample.

Upvotes: 4

Related Questions