Get screen width and height in a Fragment
This will give you what you want without needing for context or view:
import android.content.res.Resources;
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
This code also works with Fragments:
int width = getResources().getConfiguration().screenWidthDp;
int height = getResources().getConfiguration().screenHeightDp;
For comparing orientation:
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){...}
Try below modifed code of yours
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
or
Display display = getActivity().getWindowManager().getDefaultDisplay();
int stageWidth = display.getWidth();
int stageHeight = display.getHeight();
Basically you just required to put getActivity()
(to get the context) before getWindowManager()
function.