Monday, September 14, 2015

[Android][Resolved] Cannot resolve method getWindowManager

This is the code helped me get the screen width at Activity. However, when i move that from Activity to Fragment, it doesn't work anymore.



After some searching on internet, i found this code could help solve my problem:

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
double screenWidth = metrics.widthPixels;
When i was still extends the Activity class, i can use getWindowManager() method since that is a method in Activity class. you can use that within the sub-class.


To use the getWindowManager() method in fragment, you need to use the getActivity() to get the Activity which your fragment is currently associated with.



Refernece:
http://stackoverflow.com/questions/8833825/error-getting-window-size-on-android-the-method-getwindowmanager-is-undefined
http://developer.android.com/reference/android/app/Activity.html 
http://developer.android.com/reference/android/app/Fragment.html

No comments :

Post a Comment