In Android, how can I keep verticalSpacing always equal to horizontalSpacing in GridView?

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" /> 
  1. 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. enter image description here

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.

+6
source share
3 answers

You can create your own GridView class and change the setHorizontalSpacing and setVerticalSpacing as follows:

 public class CustomGridView extends GridView { public CustomGridView(Context context) { super(context); } public CustomGridView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void setHorizontalSpacing(int horizontalSpacing) { super.setHorizontalSpacing(horizontalSpacing); // Call parent class to set the vertical spacing to be equal to the horizontal spacing super.setVerticalSpacing(horizontalSpacing); } @Override public void setVerticalSpacing(int verticalSpacing) { // Prevent the derived class from modifying the vertical spacing /*super.setVerticalSpacing(verticalSpacing);*/ } } 

Then use your own class in xml as follows:

As for your second question: I couldn’t understand what you mean by “start prototyping”.

An example image of the desired result will help a lot.

0
source

you can use custome gridview text text and place the space as you want, and also set it horizontally or vertically easily.

0
source
 <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="350dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:clickable="true" android:columnWidth="80dp" android:drawSelectorOnTop="true" android:focusable="true" android:gravity="center" android:numColumns="auto_fit" android:smoothScrollbar="true" android:splitMotionEvents="true" android:stretchMode="columnWidth" android:verticalSpacing="5dp" /> and textview that is : <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="#55d3d3d3" android:gravity="center_vertical|center_horizontal" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="asdfasdadasds" android:textColor="#77FFFFFF" /> 
0
source

Source: https://habr.com/ru/post/989105/


All Articles