4

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>
fasteque
  • 4,309
  • 8
  • 38
  • 50
  • Hi @fasteque, I'm also not sure about the root cause, but did you check the width of VerticalGridView (parent of ) itself? I'm suspecting VerticalGridView itself has smaller width and aligned to center. – corochann Jul 14 '16 at 09:27
  • @corochann thanks for your quick feedback! Good point, I'll check about this point, maybe you're right. The weird things is (assuming num of columns is 6): with 1-5 items, they are centered, with for example 7 items, the one in the second row gets correctly on the left. But maybe it's because the 6 items in the first row force the VerticalGridView to fill the parent. – fasteque Jul 14 '16 at 10:09
  • @corochann you were right, I've subclassed `VerticalGridPresenter` to provide my own layout for the `VerticalGridView` where I set the width to be `match_parent`. Now the items are on the left, even if I see more empty space on the right then on left. But it's a minor issue. If you provide an actual answer here below, I'll accept it. Thanks again. – fasteque Jul 14 '16 at 11:08
  • Good that it solved! – corochann Jul 14 '16 at 12:12
  • @fasteque, can you explain how can you implement it. also, find my comment below how my current code is. – Jain Nidhi Dec 03 '18 at 08:37
  • 1
    @JainNidhi please take a look starting from here: https://github.com/fasteque/leanback-extensions/blob/master/leanback-extensions/src/main/java/com/fasteque/leanback/widget/presenter/JustifiedVerticalGridPresenter.java. It's not up to date, but it should give you some guidance. – fasteque Dec 10 '18 at 15:38

3 Answers3

4

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).

corochann
  • 1,604
  • 1
  • 13
  • 24
  • That is helpful. – skygeek Sep 18 '18 at 15:19
  • VerticalGridPresenter gridPresenter = new VerticalGridPresenter(FocusHighlight.ZOOM_FACTOR_NONE, false); gridPresenter.setShadowEnabled(false); gridPresenter.setNumberOfColumns(NUM_COLUMNS); setGridPresenter(gridPresenter); where i can set width VerticalGridView to match_parent? – Jain Nidhi Dec 03 '18 at 07:16
3

Based on the @corochann and @fasteque answer. You can initialize VerticalGridPresenter quicker use this method.

public class CustomVerticalGridPresenter extends VerticalGridPresenter {

@Override
protected void initializeGridViewHolder(ViewHolder vh) {
    super.initializeGridViewHolder(vh);
    VerticalGridView gridView = vh.getGridView();

    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    gridView.setLayoutParams(params);
}
Shu Zhang
  • 521
  • 12
  • 20
2

Just try adding android:orientation=vertical In your manifest and layout file

ashwini
  • 142
  • 8
  • How do you set `android:orientation=vertical` in manifest? There's no such attribute. You can set `android:screenOrientation` but not with `vertical` value. – fasteque Jul 07 '16 at 06:37
  • check this [SO answer](http://stackoverflow.com/a/582585/5995040) that will support adding orientation in the manifest. Hope this helps – Mr.Rebot Jul 08 '16 at 12:46
  • @Mr.Rebot you can set `portrait` not `vertical`. Also, since it's an application for the TV, I need to have it in `landscape`. – fasteque Jul 08 '16 at 13:00