List of usage examples for android.widget GridView getHorizontalSpacing
public int getHorizontalSpacing()
From source file:Main.java
public static void setGridViewSize(GridView grid) { ListAdapter myListAdapter = grid.getAdapter(); if (myListAdapter == null) { //do nothing return null return;//from w w w . j av a 2 s. c om } //set listAdapter in loop for getting final size int desiredWidth = View.MeasureSpec.makeMeasureSpec(grid.getWidth() / grid.getNumColumns(), View.MeasureSpec.AT_MOST); int totalHeight = 0; View view = null; for (int i = 0; i < ((myListAdapter.getCount() / grid.getNumColumns()) + myListAdapter.getCount() % grid.getNumColumns()); i++) { view = myListAdapter.getView(i, view, grid); if (i == 0) { view.setLayoutParams(new GridView.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); } view.measure(desiredWidth, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); totalHeight += view.getMeasuredHeight(); } //setting listview item in adapter ViewGroup.LayoutParams params = grid.getLayoutParams(); params.height = totalHeight + (grid.getHorizontalSpacing() * (((myListAdapter.getCount() - 1) / grid.getNumColumns()) + (myListAdapter.getCount() % grid.getNumColumns()))); grid.setLayoutParams(params); }
From source file:org.dmfs.android.colorpicker.PaletteFragment.java
@Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); /*/*from ww w . j a va 2s .com*/ * TODO: build the layout programmatically to get rid of the resources, so we can distribute this in a single jar */ final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.org_dmfs_colorpickerdialog_palette_grid, container, false); final GridView gridview = (GridView) rootView.findViewById(android.R.id.content); mAdapter = new PaletteGridAdapter(getActivity(), mPalette); gridview.setAdapter(mAdapter); gridview.setOnItemClickListener(this); gridview.setNumColumns(mAdapter.getNumColumns()); /* * Adjust the layout of the gridview to a square. * * Inspired by Bill Lahti, see http://blahti.wordpress.com/2012/07/23/three-variations-of-image-squares/ */ gridview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressLint("NewApi") public void onGlobalLayout() { int parentHeight = rootView.getHeight() - rootView.getPaddingTop() - rootView.getPaddingBottom(); int parentWidth = rootView.getWidth() - rootView.getPaddingLeft() - rootView.getPaddingRight(); int gridWidth = Math.min(parentWidth, parentHeight); int columnSpacing; if (android.os.Build.VERSION.SDK_INT >= 16) { columnSpacing = gridview.getHorizontalSpacing() * (mAdapter.getNumColumns() - 1); } else { /* * TODO: getHorizontalSpacing() has been introduced in SDK level 16. We need to find a way to get get the actual spacing. Until then we use * a hard coded value of 8 dip. * * One way would be to use a dimension in the layout. That would allow us to resolve the dimension here. However, that would be one step * away from a library without resource dependencies. Maybe there is an Android dimension resource with a reasonable value? */ DisplayMetrics metrics = inflater.getContext().getResources().getDisplayMetrics(); if (android.os.Build.VERSION.SDK_INT > 10) { columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics) * (mAdapter.getNumColumns() - 1); } else { // Android 2 seems to add spacing around the entire gridview columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics) * mAdapter.getNumColumns(); } } // width of a single column int columnWidth = (gridWidth - columnSpacing) / mAdapter.getNumColumns(); // estimated width of the grid int actualGridWidth = mAdapter.getNumColumns() * columnWidth + columnSpacing; // add padding to center the grid if we don't use the entire space due to rounding errors if (actualGridWidth < gridWidth - 1) { int padding = (gridWidth - actualGridWidth) / 2; if (padding > 0) { gridview.setPadding(padding, padding, padding, padding); } } else { // no padding needed gridview.setPadding(0, 0, 0, 0); } // set the column width gridview.setColumnWidth(columnWidth); android.view.ViewGroup.LayoutParams params = gridview.getLayoutParams(); if (params == null || params.height != gridWidth) // avoid unnecessary updates { LayoutParams lparams = new LinearLayout.LayoutParams(gridWidth, gridWidth); gridview.setLayoutParams(lparams); } } }); return rootView; }
From source file:ti.org.dmfs.android.colorpicker.PaletteFragment.java
@Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); /*/*from w w w . j av a2 s.com*/ * TODO: build the layout programmatically to get rid of the resources, * so we can distribute this in a single jar */ final ViewGroup rootView = (ViewGroup) inflater .inflate(RHelper.getLayout("org_dmfs_colorpickerdialog_palette_grid"), container, false); final GridView gridview = (GridView) rootView.findViewById(android.R.id.content); mAdapter = new PaletteGridAdapter(getActivity(), mPalette); gridview.setAdapter(mAdapter); gridview.setOnItemClickListener(this); gridview.setNumColumns(mAdapter.getNumColumns()); /* * Adjust the layout of the gridview to a square. * * Inspired by Bill Lahti, see * http://blahti.wordpress.com/2012/07/23/three * -variations-of-image-squares/ */ gridview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressLint("NewApi") public void onGlobalLayout() { int parentHeight = rootView.getHeight() - rootView.getPaddingTop() - rootView.getPaddingBottom(); int parentWidth = rootView.getWidth() - rootView.getPaddingLeft() - rootView.getPaddingRight(); int gridWidth = Math.min(parentWidth, parentHeight); int columnSpacing; if (android.os.Build.VERSION.SDK_INT >= 16) { columnSpacing = gridview.getHorizontalSpacing() * (mAdapter.getNumColumns() - 1); } else { /* * TODO: getHorizontalSpacing() has been introduced * in SDK level 16. We need to find a way to get get * the actual spacing. Until then we use a hard * coded value of 8 dip. * * One way would be to use a dimension in the * layout. That would allow us to resolve the * dimension here. However, that would be one step * away from a library without resource * dependencies. Maybe there is an Android dimension * resource with a reasonable value? */ DisplayMetrics metrics = inflater.getContext().getResources().getDisplayMetrics(); if (android.os.Build.VERSION.SDK_INT > 10) { columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics) * (mAdapter.getNumColumns() - 1); } else { // Android 2 seems to add spacing around the // entire gridview columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics) * mAdapter.getNumColumns(); } } // width of a single column int columnWidth = (gridWidth - columnSpacing) / mAdapter.getNumColumns(); // estimated width of the grid int actualGridWidth = mAdapter.getNumColumns() * columnWidth + columnSpacing; // add padding to center the grid if we don't use the // entire space due to rounding errors if (actualGridWidth < gridWidth - 1) { int padding = (gridWidth - actualGridWidth) / 2; if (padding > 0) { gridview.setPadding(padding, padding, padding, padding); } } else { // no padding needed gridview.setPadding(0, 0, 0, 0); } // set the column width gridview.setColumnWidth(columnWidth); android.view.ViewGroup.LayoutParams params = gridview.getLayoutParams(); if (params == null || params.height != gridWidth) // avoid // unnecessary // updates { LayoutParams lparams = new LinearLayout.LayoutParams(gridWidth, gridWidth); gridview.setLayoutParams(lparams); } } }); return rootView; }