Following is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridviewgallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="@dimen/gridview_column_width"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#444444"
/>
When I use android:stretchMode="columnWidth" , it creates a gap between the columns(though it fills the screen width) And if I use android:stretchMode="none" , it gets left aligned and there is a gap left on right side of screen(though now there is no gap between columns) Problem is I want both : To fill the screen width as well no gap between columns. How to achieve it?
Both GridView and GridLayout are safe for use in production code. They are obsolete, and I would not recommend that anyone use them, but they are safe.
android:numColumns: This attribute of GridView will be used to decide the number of columns that are to be displayed in Grid. android:horizontalSpacing: This attribute is used to define the spacing between two columns of GridView.
Let us not get confused with GridView and GridLayout to be the same. GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.
A combination of android:numColumns="auto_fit"
android:stretchMode="columnWidth"
and android:columnWidth="60dp"
will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See example below...
<GridView
android:id="@+id/tablegrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff7965"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:columnWidth="60dp"
/>
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