Search code examples
androidandroid-layoutandroid-tvleanback

Android TV: VerticalGridFragment row items alignment


I'm facing an issue subclassing VerticalGridFragment in my Android TV project: everything works fine but if in one row there are less items then the maximum number of columns, then the items are horizontally aligned starting from the center.

The weird thing is that it happens only if the total number of elements are less then the maximum number of columns. For example, assuming this number is 6, if I have 1 item, it's placed at the center of the row. Instead, if I have 14 items, the 2 items on the third row are placed starting from the left.

issue

Is there a way to align the items in a row to the left or start? Am I doing something wrong?

I've tried in this way but nothing happens, items are aligned starting from the center.

<style name="AppTheme.Widget.Leanback.GridItems.VerticalGridView"
       parent="Widget.Leanback.GridItems.VerticalGridView">
    <item name="android:gravity">left|start</item>
</style>

And then set it like this in my theme, which extends from Theme.Leanback:

<item name="itemsVerticalGridStyle">@style/AppTheme.Widget.Leanback.GridItems.VerticalGridView</item>

Solution

  • Maybe the VerticalGridView itself, parent of <item>, has smaller width and aligned to center.

    You can try to set the width of VerticalGridView to be match_parent (see comments of fasteque).