android how to get height and width in onCreateView
android, layout
Solution
i think i have it
i think getSize returns the 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'll have to wait and see what my customers say
Problem
i'm creating my view programmatically, no xml at all and need the real size of the area i'm working in. i have been using 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 that doesn't take into account all the other 'decoration' like title bar and nav bar the docs say not to 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 that returns 0 i guess the issue is the view hasn't been created yet but there must be a parent to onCreateView that knows how big the view will be right? how do i find that? 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 it doesn't seem like a clean way to do it. i guessed the xhigh and xxhigh, i haven't found the actual numbers yet ALSO i should add that the view is created from onTabSelected in the 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(); } ```