I want to make number of rows in Gridview dynamic depending upon the screen size of the android phone. so i need height of the Gridview in onCreate() to count number of rows.(height / colums) here is a code snippet::
public class GridActivity extends Activity
{
GridView gridView;
GridAdapter adapter;'
public void onCreate(Bundle savedInstance state)
{
setContentView(R.layout.gridview);
gridView = (GridView) findViewById(R.id.grid);
adapter = new GridAdapter(this);
gridView.setAdapter(adapter);
int gridHeight = gridView.getMeasuredHeight();
Log.d("tag"," height :: " + gridHeight);
}
}
In log cat it shows — height :: 0
I don’t understand y it gives zero after properly inflating.
Is this a bug? Or I am missing something?
GridView would return 0 measuredheight and 0 height unless it is drawn to window, so you can not get height of GridVIew at the point, A workaround of your problem may be:
Add your Nth view to gridView by comparing total height of device with height of GridView occupied after n-1 views has been drawn to screen.