This is how to change the number of columns of a GridView at run time.
int GRID_ITEM_WIDTH = 300; // the rough width of the grid item.
mGridView = (GridView) findViewById(R.id.gridView);
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
boolean isFirst = true;
@Override
public void onGlobalLayout() { // this callback called when the width of view is available
if (isFirst) { // this callback would be called several times, run the following code only at the first time it called
isFirst = false;
int width = mGridView.getWidth();
int numColumns = 1;
if(width > GRID_ITEM_WIDTH){
numColumns = width/GRID_ITEM_WIDTH;
}
mGridView.setNumColumns(numColumns);
}
}
}
);