How to make GridView use the whole screen (regardless of screen size)?

I have the following layout file that has a GridView and ImageView behind which as a 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 that I use for the actual element in each grid cell:

 <?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> 

Currently, my device displays the following:

enter image description here

Is there a way to make each element in the GridView larger so that it matches the screen size and I don't have an unused space at the bottom of the display?

This works fine on the emulator, but on the device, the screen resolution is higher, therefore, you get empty space at the bottom.

Many thanks

+7
source share
3 answers

Not automatically.

In particular, your cells are text. Android is not quite able to guess how large the text should be to achieve your goals, especially after you consider word wrap.

The GridView must have โ€œunused blank space at the bottom of the screenโ€ if you donโ€™t have enough data to fill the screen so that it can flexibly handle several screen sizes and also accommodate scrolling if there is more data. If you're aiming for something similar to a dashboard template, consider using DashboardLayout or something like that.

+3
source

This will work well. Do not forget about the vertical distance.

 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; } 
+16
source

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 this helps?

0
source

All Articles