Question

I have 2 columns in my GridLayout. What I want to do is make those columns take up half of the width of the screen each and then have its child contents fill their own cells width/height. I tried setting the children to fill_parent but that just causes the first one to take over the entire layout. And it seems GridLayout doesn't support weight? Maybe there is a better layout to use, but I want a Grid style layout so that seems like the natural choice.

Was it helpful?

Solution

This code is available on pre API21 with support library!

I have a simple piece of code to show 4 buttons in a gridLayout of 2 columns that take 50% of the available space: perhaps it can help

<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />



</GridLayout>

Solution is perhaps this :

android:layout_gravity="fill"
android:layout_columnWeight="1"

OTHER TIPS

For pre API 21, use support library:

add

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

to your dependencies.

Then in your xml file:

<android.support.v7.widget.GridLayout
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:columnCount="2"
                    app:orientation="horizontal"
                    app:rowCount="1">

                    <TextView
                        android:text="1"
                        android:textStyle="bold"
                        app:layout_columnWeight="1"
                        />

                    <TextView
                        android:text="2"
                        android:textStyle="bold"
                        app:layout_columnWeight="1" />

</android.support.v7.widget.GridLayout>

Here note the use of "app" prefix and dont forget to add

xmlns:app="http://schemas.android.com/apk/res-auto"

to your xml file

adding views dynamically in a grid Layout of 2 columns that take 50% of the available space:

GridLayout gridLayout = new GridLayout();

View view; //it can be any view

GridLayout.LayoutParams param = new GridLayout.LayoutParams();

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f);

param.width = 0;

view.setLayoutParams(param);

gridLayout.add(view);

in static way (in .xml file).

<android.support.v7.widget.GridLayout
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:alignmentMode="alignBounds"
   app:columnCount="2"
   app:columnOrderPreserved="false"
   app:orientation="horizontal"
   android:layout_margin="20dp"
   android:layout_marginBottom="30dp"
   android:padding="4dp"
   app:rowCount="2">

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_username"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/user_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_first_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/first_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_middle_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/middle_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_last_name" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/last_name" />

</android.support.v7.widget.GridLayout>

Ok, so I gave up on the grid view and just used a some linear layouts. I made a vertical one and then added 2 horizontals. It's slightly more involved than the grid view... but until I figure that out at least this works.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/btn_mybutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionmybutton"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/btn_prefs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionSettings"
            android:src="@drawable/ic_settings" />

    </LinearLayout>

</LinearLayout>

And then i add this to make the buttons square :)

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);

  btnPrefs.setMinimumHeight(btnPrefs.getWidth());
  btnVerse.setMinimumHeight(btnMyButton.getWidth());

 }

You could extend the RelativeLayout class (or use LinearLayout if you want to) to make sure the height of the item will be the same as the height.

public class GridItem extends RelativeLayout {

        public GridItem(Context context) {
                super(context);
        }

        public GridItem(Context context, AttributeSet attr) {
                super(context, attr);
        }

        public GridItem(Context context, AttributeSet attr, int integer) {
                super(context, attr, integer);
        }

        // Override onMeasure to give the view the same height as the specified width
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
        }

}

The parent view of the item layout should be the GridItem view to make sure it works. This must be the layout file you'll inflate in the getView of your ListAdapter

<?xml version="1.0" encoding="utf-8"?>
<my.packagename.GridItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The content of the item -->

</my.packagename.GridItem>

And set the stretchMode of the GridView to columnWidth. This will make all items fit to the width specified number of columns. The new view will make sure they will also have the same height.

<GridView
    android:id="@+id/gridList"
    android:numColumns="2"
    android:stretchMode="columnWidth"
/>

When you use a GridLayoutManager you are able to use setSpanSizeLookup. Here is a snippet from my project which should help to use this method properly:

if (mAdapter == null) {
    final int columnCount = getResources().getInteger(R.integer.numberGridColumns);
    mLayoutManager = new GridLayoutManager(getActivity(), columnCount);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (mAdapter.getItemViewType(position)) {
                case ListAdapter.VIEW_TYPE_ONE_COLUMN:
                    return columnCount;
                case RecipeListAdapter.VIEW_TYPE_FULL_COLUMN:
                default:
                    return 1;
            }
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecipeListAdapter(mPresenter);
    mRecyclerView.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top