Android - Center Gallery elements vertically

99 Views Asked by At

I'm using a library called FancyCoverFlow which extends Gallery. I tried adding :

android:gravity="center"

in the layout to center child elements with no luck. The gallery is stretched on the entire screen, I made its background red to make sure.
How do you center Gallery child elements vertically ?

2

There are 2 best solutions below

0
On BEST ANSWER

The library is no longer maintained but I'm posting the solution anyway.
The specific line you have to modify is the last one in the method dispatchDraw(Canvas canvas) in FancyCoverFlowItemWrapper class :

From :

canvas.drawBitmap(this.wrappedViewBitmap, (float)((this.getWidth() - childView.getWidth()) / 2), 0, this.paint);

To :

canvas.drawBitmap(this.wrappedViewBitmap, (float)((this.getWidth() - childView.getWidth()) / 2), (float)((this.getHeight() - childView.getHeight()) / 2), this.paint);

Just instead of drawing the view at top = 0 you calculate the middle and use it instead.
FancyCoverFlowItemWrapper instantiation resides in FancyCoverFlowItem class, so normally you would extend it and override the said method, but it's not public, so you have no access to it.

I ended up duplicating instead or extending the classes :

  • FancyCoverFlow -> MyFancyCoverFlow
  • FancyCoverFlowAdapter -> MyFancyCoverFlowAdapter
  • FancyCoverFlowItemWrapper -> MyFancyCoverFlowItemWrapper

So now in my xml I don't use FancyCoverFlow anymore but MyFancyCoverFlow instead.

Here's a fork of the fixed bug : https://github.com/Mehdiway/FancyCoverFlow

2
On

I tried this lib like you, but no luck. So I suggest you just put it inside a layout (RelativeLayout, FrameLayout, LinearLayout...) with match parent width and height, then set paddingTop to your FancyCoverFlow.