Is there a way to get the row count of a GridView for Android API Level 8?
Use Property of RowCount to get the number of rows in a Data Grid View.
To get the row number, you can use the GridView. RowCount property. To sort the grid against a column, you can use end-user capabilities (click or right-click a column header).
I had to solve this last night and it worked for me. It looks up a child's width and assuming all cells have the same width, divides the GridView's width by the child's width. (getColumnWidth()
is also unavailable in earlier APIs, so hence the workaround).
private int getNumColumnsCompat() {
if (Build.VERSION.SDK_INT >= 11) {
return getNumColumnsCompat11();
} else {
int columns = 0;
int children = getChildCount();
if (children > 0) {
int width = getChildAt(0).getMeasuredWidth();
if (width > 0) {
columns = getWidth() / width;
}
}
return columns > 0 ? columns : AUTO_FIT;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private int getNumColumnsCompat11() {
return getNumColumns();
}
However there are some important restrictions to note with this.
setHorizontalSpacing()
. If you use horizontal spacing you may need to tweak this to account for it.I solved the lack of getNumColumns in V8, in the case where the number of columns is specified in resource, by declaring an integer resource and using that in the GridView resource and code.
<integer name="num_grid_columns">2</integer>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:columnCount="@integer/num_grid_columns"
app:orientation="horizontal" >
int cols = getResources().getInteger(R.integer.num_grid_columns);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With