Get screen width and height in a Fragment
android, android-fragments
Solution
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.
Problem
If I extend activity in my app I can get width and height: ``` DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int wwidth = displaymetrics.widthPixels; ``` or ``` Display display = getWindowManager().getDefaultDisplay(); stageWidth = display.getWidth(); stageHeight = display.getHeigth(); ``` But at present I extend fragment and I can't use the above code to get the width.