Android TV: VerticalGridFragment row items alignment
Asked Answered
Y

3

5

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>
Yet answered 7/7, 2016 at 6:31 Comment(6)
Hi @fasteque, I'm also not sure about the root cause, but did you check the width of VerticalGridView (parent of <item>) itself? I'm suspecting VerticalGridView itself has smaller width and aligned to center.Cowling
@Cowling 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.Yet
@Cowling 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.Yet
Good that it solved!Cowling
@fasteque, can you explain how can you implement it. also, find my comment below how my current code is.Legion
@JainNidhi please take a look starting from here: github.com/fasteque/leanback-extensions/blob/master/…. It's not up to date, but it should give you some guidance.Yet
C
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).

Cowling answered 14/7, 2016 at 12:15 Comment(2)
That is helpful.Cristoforo
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?Legion
D
5

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);
}
Dynamic answered 21/12, 2018 at 4:35 Comment(0)
C
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).

Cowling answered 14/7, 2016 at 12:15 Comment(2)
That is helpful.Cristoforo
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?Legion
C
2

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

Clinician answered 7/7, 2016 at 6:35 Comment(3)
How do you set android:orientation=vertical in manifest? There's no such attribute. You can set android:screenOrientation but not with vertical value.Yet
check this SO answer that will support adding orientation in the manifest. Hope this helpsNeutrino
@Neutrino you can set portrait not vertical. Also, since it's an application for the TV, I need to have it in landscape.Yet

© 2022 - 2024 — McMap. All rights reserved.