Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Centering Item in RecyclerView

Im a newbie in android. Im not sure why I cant center my item in RecyclerView.

What I want is like below image :-

Actual

What android render is like below image :-

Reality

Is there a way to push items in RecyclerView to center? So it will look like this :-

i want like this

I also provide the layout files as below :-

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content" android:layout_height="wrap_content"     android:id="@+id/calendar_itemContainer">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="April"         android:id="@+id/calendar_txtMonth"         android:layout_alignParentTop="true"         android:layout_alignParentLeft="true"         android:layout_alignParentStart="true"         android:layout_alignParentRight="true"         android:layout_alignParentEnd="true"         android:textColor="#ff58636d"         android:textSize="16sp"         android:gravity="center|center_vertical|center_horizontal"         android:paddingTop="8dp"         android:paddingLeft="12dp"         android:paddingRight="12dp" />      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="21"         android:id="@+id/calendar_txtDay"         android:layout_alignParentLeft="true"         android:layout_alignParentStart="true"         android:layout_below="@+id/calendar_txtMonth"         android:layout_alignParentRight="true"         android:layout_alignParentEnd="true"         android:textColor="#ff58636d"         android:textSize="40sp"         android:layout_marginTop="-10dp"         android:paddingLeft="10dp"         android:paddingRight="10dp"         android:paddingBottom="5dp"         android:gravity="center|center_vertical|center_horizontal" /> </RelativeLayout> 

fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent">      <android.support.v7.widget.RecyclerView         android:id="@+id/calendar_recycler_view"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:scrollbars="horizontal"         android:background="#ff2c3e50" /> </RelativeLayout> 

and the java codes :-

CalendarAdapter mAdapter = new CalendarAdapter(mDataset); mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter.setCalendarCallbacks(this); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); selectItem(mCurrentSelectedPosition); 
like image 948
skycrew Avatar asked Mar 18 '15 09:03

skycrew


2 Answers

set recyclerview to:

android:layout_width="wrap_content" android:layout_gravity="center_horizontal" 

fixed it for me

like image 164
Maxi Avatar answered Sep 24 '22 03:09

Maxi


I was trying to do what I think is the same thing you're asking when I came across this post. Here's the solution I ended up using.

Create your own subclass of LinearLayoutManager and override getPaddingLeft() and getPaddingRight() like so.

public class CustomLayoutManager extends LinearLayoutManager {     private int mParentWidth;     private int mItemWidth;      public CustomLayoutManager(Context context, int parentWidth, int itemWidth) {         super(context);         mParentWidth = parentWidth;         mItemWidth = itemWidth;     }      @Override     public int getPaddingLeft() {         return Math.round(mParentWidth / 2f - mItemWidth / 2f);     }      @Override     public int getPaddingRight() {         return getPaddingLeft();     } } 

parentWidth should be the width of the RecyclerView and itemWidth should be the width of each of your child views, both in pixels. Obviously, this only works if you know that all your child views will be the same width.

Then, just use this layout manager with your RecyclerView

like image 23
majormajors Avatar answered Sep 21 '22 03:09

majormajors