Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gridview android:numColumns="auto_fit" always create only two columns

Hi I am developing small android application in which I want to display simple gridview with some elements.Its working properly. Only problem is that it is always showing only two columns even though there is space. Its is equally dividing screen into 2 columns and displaying only two elements.If I set number of columns as number i.e. not auto_fit then it is showing properly. My code looks like:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >
<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="5dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp">
</GridView>
</FrameLayout>

and my grid element looks like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.androidcardlayout.MainActivity" >

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="100dp"
    android:layout_height="150dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout 
        android:id="@+id/cardMianRlt"
        android:layout_width="100dp"
        android:layout_height="150dp"
        >
    </RelativeLayout>

Am I doing anything wrong? Need Some help. Thank you.

like image 826
nilkash Avatar asked Dec 08 '14 06:12

nilkash


1 Answers

It looks like the auto-fit setting only applies if you have fixed column widths. Here is the only place in GridView source code that uses the auto-fit setting:

private boolean determineColumns(int availableSpace) {
    final int requestedHorizontalSpacing = mRequestedHorizontalSpacing;
    final int stretchMode = mStretchMode;
    final int requestedColumnWidth = mRequestedColumnWidth;
    boolean didNotInitiallyFit = false;

    if (mRequestedNumColumns == AUTO_FIT) {
        if (requestedColumnWidth > 0) {
            // Client told us to pick the number of columns
            mNumColumns = (availableSpace + requestedHorizontalSpacing) /
                    (requestedColumnWidth + requestedHorizontalSpacing);
        } else {
            // Just make up a number if we don't have enough info
            mNumColumns = 2;
        }
    }

This private function is called during the measure / layout process. Notice that within the auto-fit if statement, unless requestedColumnWidth > 0, then you just get 2 columns, which is what you are seeing.

If a fixed width works for your app, then you need to put it in your XML like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp"
        android:numColumns="auto_fit"
        android:columnWidth="30dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp">
    </GridView>
</FrameLayout>
like image 161
Bruce Avatar answered Nov 11 '22 19:11

Bruce