Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get columncount in gridview

How to get column count in Gridview when gridview noofColumn="autofit" mode

How to get single row no of column

<GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"

                android:background="@android:color/white"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth" />
like image 579
Sanket990 Avatar asked Dec 03 '25 01:12

Sanket990


2 Answers

You can get column count in java code..

GridView mGridView = (GridView)findViewById(R.id.gridview);
int column_coutnt = mGridView.getNumColumns();

Second Way:

int columns = 0;
int children = mGridView.getChildCount();
if (children > 0) {
     int width = mGridView.getChildAt(0).getMeasuredWidth();
     if (width > 0) {
        columns = mGridView.getWidth() / width;
     }
 }            
like image 68
Niranj Patel Avatar answered Dec 05 '25 14:12

Niranj Patel


Try like this..

Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );
like image 29
Giridharan Avatar answered Dec 05 '25 14:12

Giridharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!