Note that there are some explanatory texts on larger screens.

plurals
  1. POGridview and column size on multiple devices
    primarykey
    data
    text
    <p>I'm trying to create a gridview that fits perfectly whatever the screensize is. When I read the many examples or SO answers about this subject, the only thing that I found is : put some "dimensions.xml" files in each values-size folder and let gridview adapt the number of column and automagic spacing. The problem is that sometimes, the spacing is too big and make the result really ugly. </p> <p>My question is : what if I don't want to put a size and want to manage the gridview with a pixel precision ? Any example would be welcomed. </p> <p>Just to be clear : for example I want x columns with 4 pixels spacer between each columns, so I need to calculate the better size of the columns for that and specify it to the gridview. The idea is to render things like in google play store app where there is always the same space between gridelement on every device (the column is adjusted to the screen size).</p> <p>Edit : here's the code I'm using right now... It comes from an example of Romain Guy where he shows how to display a grid view.</p> <pre><code> // This listener is used to get the final width of the GridView and then calculate the // number of columns and the width of each column. The width of each column is variable // as the GridView has stretchMode=columnWidth. The column width is used to set the height // of each view so we get nice square thumbnails. mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (mAdapter.getNumColumns() == 0) { final int numColumns = (int) Math.floor((double) mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing)); if (numColumns &gt; 0) { final int columnWidth = mGridView.getWidth() / numColumns - mImageThumbSpacing; mAdapter.setNumColumns(numColumns); mAdapter.setItemHeight(columnWidth); } } } }); </code></pre> <p>mImageThumbSize and mImageThumbSpacing comes from dimension files. What I'm looking for is a way to calculate automatically the best number of columns according to the screen size. For example : low res phone 2 columns, phone 3 columns, small tablet 4, tablet 5, big tablet 6. The main issue is that a phone can give the same gridview size than a tablet so I can't rely on pure screen size calculation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload