7

I've got the following layout file, which has a GridView and an ImageView behind that as the background.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginRight="-70dp"
            android:layout_marginBottom="-50dp"
            android:src="@drawable/s_background"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/gridview"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:columnWidth="90dp"
                  android:numColumns="auto_fit"
                  android:verticalSpacing="10dp"
                  android:horizontalSpacing="10dp"
                  android:stretchMode="columnWidth"
                  android:gravity="center"/>
    </LinearLayout>
</FrameLayout>

And this is the layout I use for the actual item in each "cell" of the grid :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:id="@+id/cardInGrid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:singleLine="true"
            android:textSize="40sp"
            android:textColor="#660099"
            android:typeface="serif"/>
</LinearLayout>

I'm seeing the following on my device at the moment :

enter image description here

Is there any way of making each item in the GridView larger, so it fits the size of the screen and I don't have un-used white space at the bottom of the display?

This works fine on an emulator, but on a device the screen resolution is higher, hence getting the white space at the bottom.

Many thanks

George Kagan
  • 5,207
  • 8
  • 44
  • 49
Jimmy
  • 15,225
  • 38
  • 120
  • 211

3 Answers3

16

This will work well. Don't forget about vertical spacing.

public class MyAdapter extends BaseAdapter {
    public static int ROW_NUMBER = 5;

    public MyAdapter (Context mContext, ArrayList<String> list) {
        this.context = mContext;
        lstDate = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
        }
        // we need to decrease cell height to take into account verticalSpacing for GridView
        int cellHeight = StrictMath.max(parent.getHeight() / ROW_NUMBER - parent.getContext().getResources().getDimensionPixelOffset(R.dimen.grid_spacing), 320);
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                parent.getHeight() / ROW_NUMBER);
        convertView.setLayoutParams(param);

        return convertView;
    }
soshial
  • 3,252
  • 4
  • 27
  • 36
Promes_TTT
  • 161
  • 1
  • 2
  • 1
    It may not work if the adapter has data available before the view appears. The first views may be fetched before the parent even has an height. Also, you don't have to get the reference to the `GridView` in your adapter: the `parent` parameter contains it already. – Marc Plano-Lesay Apr 06 '17 at 10:06
3

Not automatically.

In particular, your cells are text. Android is not exactly in position to guess how big the text should be to accomplish your aims, particularly once you take word-wrap into account.

The point of GridView is to have "un-used white space at the bottom of the display", if you do not have enough data to fill the screen, so that it can flexibly handle multiple screen sizes and also accommodate scrolling if there is more data. If you are aiming for something akin to the dashboard pattern, consider using DashboardLayout or something along those lines.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    DashboardLayout redirects at GitHub, not Android! This an important detail, for me at least... – Radu Mar 25 '13 at 13:00
0

Have you tried android:layout_weight="1"?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>

Or

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
android:layout_weight="1">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
/>
</LinearLayout>

Hope that helps?

wired00
  • 12,270
  • 6
  • 65
  • 65
  • I've tried those, unfortunately none of them seem to have any effect. :( – Jimmy Apr 16 '11 at 23:21
  • damn : ( maybe its just not possible with a gridview like mark says a standard gridview is just designed to show items from top down with whitespace below – wired00 Apr 16 '11 at 23:24