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


i have declared girdview in xml layout shown 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 show fixed size images(vertical rectangle not square) in gridview. have set columnwidth auto_fit horizontal space can used optimally gridview. per available screen size. have set stretchmode spacingwidthuniform columns placed @ equal distance sides.

i want set verticalspacing equal horizontalspacing. can't in xml here because @ runtime horizontalspacing depend on space available on screen. also, when device orientation changed, horizontalspacing change.

my questions are:

  1. how can make sure verticalspacing equal horizontalspacing?

  2. how can make gridview start layouting first column beginning plus padding , end layouting last column end of gridview minus padding?

edit 1

i tried @simas solution use mentioned custom grid view. that,

  1. i copied class source directory.

  2. included grid view in layout xml 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. set adapter gridview shown below

     customgridview moviesgridview = (customgridview) rootview.findviewbyid(r.id.movies_gridview); moviesgridview.setadapter(new moviesadapter(rootview.getcontext().getapplicationcontext(), mmovies)); 

it still shows different spacing columns , rows shown below. enter image description here

edit 2

@simas in second question 'start layouting' mean gridview should start drawing first column @ start of gridview , last column of gridview should end @ end of gridview. also, columns , rows should have equal spacing. let me know if clarifies question more.

you can create custom gridview class , modify functionality of sethorizontalspacing , setverticalspacing this:

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 set vertical spacing equal horizontal spacing         super.setverticalspacing(horizontalspacing);     }      @override     public void setverticalspacing(int verticalspacing) {         // prevent derived class modifying vertical spacing         /*super.setverticalspacing(verticalspacing);*/     }  } 

then use custom class in xml this:

as second question: couldn't understand meant "start layouting".

an image example of desired result lot.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -