Android how to get height and width in onCreateView

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(); // 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; 

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) { // When the given tab is selected, show the tab contents in the // container view. Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); fragment.setArguments(args); getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); } 
+4
source share
1 answer

I think I have

I think getSize returns realsize minus the navigation bar, so I need to subtract the action bar

so now i do this:

 @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.getSize(size); int width = size.x; int height = size.y; TypedValue tv = new TypedValue(); int actionBarHeight=0; if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } height -= actionBarHeight; //Log.d("MainActivity", "onCreateView tabNo="+tabNo +" display width="+width+" height="+height+" rotation="+rotation); switch(tabNo){ case 1: return onCreateViewScoreFragment(context, width, height); case 2: return onCreateViewFilesFragment(context, inflater, width>height); default: return onCreateViewSettingsFragment(context, inflater, width>height); } } 

It works on my tablet, I'm not 100% sure about other devices, I will have to wait and see what my clients say.

+3
source

All Articles