Get actual GridView column width in Android

Hi, fellow developers.

I have a Grid View in an Android app. The grid has a parameter that automatically matches the number of columns and has a minimum column width of 250 pixels.

Inside the grid cells, I have a LinearLayout that has an image view and some textual representations.

I want my Image View to always be square, but unfortunately I was not able to do this with any layout settings (open the sentences here if there is an option that I missed).

So my idea is that I need to somehow get the width of the grid column in my image. Load the class and dynamically change the height of the imageView to fit the width.

I could not get the column width. If I set breakPoint in my public View getView(int position, View convertView, ViewGroup parent) , I see that my parent ViewGroup is a Grid and has the correct column width value, but when I use:

 GridView mGridView = (GridView)parent; int width = mGridView.getColumnWidth(); 

the application crashes because it says that there is no getColumnWidth method.

Any help here?

+4
source share
3 answers

The solution could be extending your ImageView and overriding the onMesure () method to suit your requirement:

 onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec,heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(),getMeasuredWidth()); } 
+5
source

Just in case, someone is looking for a way to get the actual pre api 16 width column (as I did when I saw this post), here is how I retrieve it.

I expanded the GridView and tried the getColumnWidth method with this

 @SuppressLint("NewApi") @Override public int getColumnWidth() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) return super.getColumnWidth(); else { try { Field field = GridView.class.getDeclaredField("mColumnWidth"); field.setAccessible(true); Integer value = (Integer) field.get(this); field.setAccessible(false); return value.intValue(); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } 
+6
source

An alternative to Daniel Bo's answer is to manually calculate the value:

 private int getColumnWidth(Context context, GridView gridView) { if (android.os.Build.VERSION.SDK_INT >= 16) return gridView.getColumnWidth(); Resources res = context.getResources(); int lPad = (int)res.getDimension(R.dimen.left_padding); int rPad = (int)res.getDimension(R.dimen.right_padding); int hSpace = (int)res.getDimension(R.dimen.horizontal_spacing); int cols = gridView.getNumColumns(); int width = gridView.getWidth(); return (width - lPad - rPad + hSpace) / cols - hSpace; } 

Note that this only works for APIs above 11 (Honeycomb) and assumes that you configured GridView with some values ​​defined as resources. Consider, for example, that you have a define.xml that has:

 <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="left_padding">4dp</dimen> <dimen name="right_padding">8dp</dimen> <dimen name="horizontal_spacing">2dp</dimen> </resources> 

And then your layout xml file has:

 <GridView ... android:horizontalSpacing="@dimen/horizontal_spacing" android:paddingLeft="@dimen/left_padding" android:paddingRight="@dimen/right_padding" ... /> 

And if you have many similar GridViews, it would be nice to put these properties in a custom style.

At least it worked for me without using reflection. Note that it will probably be too difficult to call this function often.

Hope this helps someone.

+1
source

All Articles