I declared GirdView in xml layout as below.
<GridView android:id="@+id/movies_gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="true" android:columnWidth="92dp" android:numColumns="auto_fit" android:stretchMode="spacingWidthUniform" android:gravity="center_horizontal" />
I intend to display images with a fixed size (the vertical rectangle is not a square) in this GridView. I set columnWidth to auto_fit so that the horizontal space can be optimally used using the GridView. According to available screen size. I set stretchMode to spacingWidthUniform so that the columns are equally spaced on all sides.
I want to set verticalSpacing to horizontalSpacing . I cannot do this in xml here, because at run time horizontalSpacing will depend on the space available on the screen. In addition, when you change the orientation of the device, horizontalSpacing changes.
My questions:
How can I make sure that verticalSpacing will always be equal to horizontalSpacing ?
How to make a GridView start the layout of its first column from the beginning plus addition and end of the layout of the last column to the end of the GridView minus filling?
EDIT 1
I tried the @simas solution to use the mentioned custom grid view. For this
I copied this class to the source directory.
Includes this kind of grid in the xml layout, as shown below.
<com.aviras.ashish.popularmoviesapp.widgets.CustomGridView android:id="@+id/movies_gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="@dimen/grid_item_width" android:drawSelectorOnTop="true" android:gravity="center_horizontal" android:numColumns="auto_fit" android:stretchMode="spacingWidthUniform" tools:listitem="@layout/grid_item_movie_poster" />
Install the adapter in this gridview as shown below
CustomGridView moviesGridView = (CustomGridView) rootView.findViewById(R.id.movies_gridview); moviesGridView.setAdapter(new MoviesAdapter(rootView.getContext().getApplicationContext(), mMovies));
It still shows different spacing for columns and rows, as shown below. 
EDIT 2
@simas in the second question with 'start layouting' I mean that the GridView should start drawing the first column at the beginning of the GridView , and the last column of the GridView should end at the end of the GridView . In addition, columns and rows must have equal spacing. Let me know if this clarifies my question again.
source share