thomas.cloud
thomas.cloud

Reputation: 963

How do I find the screen size in a fragment class

I was looking at this posting: Get screen dimensions in pixels when I was trying to determine the size of the device's screen while in a fragment class. One answer was close to what I needed but the only code that ended up working for me was:

WindowManager wm = (WindowManager) getView().getContext().getSystemService(Context.WINDOW_SERVICE); 
Display screen = wm.getDefaultDisplay();  

whereupon I could then use getHeight(); or another non-deprecated term.

Upvotes: 4

Views: 5326

Answers (1)

Manfred Moser
Manfred Moser

Reputation: 29912

You questions contains the answer! Just use getActvity() as the context in the fragment.

Upvotes: 8

Related Questions