I create my presentation programmatically, not xml at all and need the real size of the area I work in. I used this code:
public static class DummySectionFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } Context context = getActivity(); int tabNo = getArguments().getInt(ARG_SECTION_NUMBER); Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
but this does not take into account all other βdecorationsβ, such as the title bar and navigation bar
The docs say don't use display metrics to get the size, but how can I find out the size of the view in onCreateView? I tried container.getHeight () but returns 0
I assume the problem is that the view has not been created yet, but there must be a parent onCreateView element that knows how big the view will be?
how do i find this?
thanks
FYI, this works:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } Context context = getActivity(); int tabNo = getArguments().getInt(ARG_SECTION_NUMBER); Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); // check display size to figure out what image resolution will be loaded DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); Point size = new Point(); display.getRealSize(size); display.getSize(size); int width = size.x; int height = size.y; switch (metrics.densityDpi) { case DisplayMetrics.DENSITY_XXHIGH: height -= 72; break; case DisplayMetrics.DENSITY_XHIGH: height -= 64; break; case DisplayMetrics.DENSITY_HIGH: height -= 48; break; case DisplayMetrics.DENSITY_MEDIUM: height -= 32; break; case DisplayMetrics.DENSITY_LOW: height -= 24; break; default: Log.e("onCreateView", "Unknown density"); } }
but that doesn't seem like a clean way to do it. I guessed about xhigh and xxhigh, I haven't found the actual numbers yet
ALSO I must add that the view is created from onTabSelected in MainActivity, like this
@Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
source share