Leanback VerticalGridFragment remove top spacing
Asked Answered
A

5

14

I am using VerticalGridFragment to display items in a grid-like layout I don't need to show search or title and I want the rows to start from top of the screen without any margins. Any help?example in the image below

Alacrity answered 18/8, 2016 at 9:13 Comment(9)
Could you show the screen shot please.Precast
@jaydroider I edited my question, check the screenshotAlacrity
You mean you want to move this rows to just below top section right ?Precast
yes, i was able to remove the titleview section and give it height=0, but i still can't figure out a way to move the rows upAlacrity
Could you tell me this is the full screen which you provided or you have left side items also connected with rows ? or simply you have this much rows only in Browse Fragment.Precast
this is fullscreen, i want items to be coming from bottom of the screen, nothing on the left, i don't need the headers, I disabled themAlacrity
the concept is for a TV channel list, when the user presses OK button, he will see this menu with his channel list, the reason why i need it like this, is for better user experience (not covering the TV area, he can still watch TV while going through channel list)Alacrity
Follow my answer below and let me know if you face any issue.Precast
Have you tried my answer ? have you get it working or not ? Let me know if you face any issue or have any doubt related to my answer.Precast
F
7

You need create new class with name CustomVerticalGridPresenter and put followig code in it.

public class CustomVerticalGridPresenter extends VerticalGridPresenter {

    VerticalGridView gridView;

    CustomVerticalGridPresenter(int zoom, boolean val){
        super(zoom, val);
    }

    @Override
    protected void initializeGridViewHolder(ViewHolder vh) {
        super.initializeGridViewHolder(vh);
        gridView = vh.getGridView();
        int top = 20;//this is the new value for top padding
        int bottom = gridView.getPaddingBottom();
        int right = gridView.getPaddingRight();
        int left = gridView.getPaddingLeft();
        gridView.setPadding(left,top,right,bottom);
    }
}

Then in verticalgridfragment class use

CustomVerticalGridPresenter videoGridPresenter = new 
CustomVerticalGridPresenter(ZOOM_FACTOR, false);

instead of

VerticalGridPresentervideoGridPresenter = new VerticalGridPresenter(ZOOM_FACTOR, false);
Filberte answered 12/12, 2018 at 6:32 Comment(0)
A
14

I found a way to do it by overriding the VerticalGridPresenter of the VerticalGridFragment then getting the VerticalGridView, and setting top padding to a smaller value. In the CustomVerticalGridPresenter class (that extends VerticalGridPresenter), override this method:

@Override
protected void initializeGridViewHolder(ViewHolder vh) {
    super.initializeGridViewHolder(vh);
    gridView = vh.getGridView();
    int top= 20;//this is the new value for top padding
    int bottom = gridView.getPaddingBottom();
    int right = gridView.getPaddingRight();
    int left = gridView.getPaddingLeft();
    gridView.setPadding(left,top,right,bottom);
}

Then in the VerticalGridFragment, assign the new CustomVerticalGridPresenter as following:

 CustomVerticalGridPresenter gridPresenter = new CustomVerticalGridPresenter();
    gridPresenter.setNumberOfColumns(NUM_COLUMNS);
    setGridPresenter(gridPresenter);
Alacrity answered 24/8, 2016 at 5:50 Comment(1)
yes it works. but you need to explain your answer in better way.Filberte
F
7

You need create new class with name CustomVerticalGridPresenter and put followig code in it.

public class CustomVerticalGridPresenter extends VerticalGridPresenter {

    VerticalGridView gridView;

    CustomVerticalGridPresenter(int zoom, boolean val){
        super(zoom, val);
    }

    @Override
    protected void initializeGridViewHolder(ViewHolder vh) {
        super.initializeGridViewHolder(vh);
        gridView = vh.getGridView();
        int top = 20;//this is the new value for top padding
        int bottom = gridView.getPaddingBottom();
        int right = gridView.getPaddingRight();
        int left = gridView.getPaddingLeft();
        gridView.setPadding(left,top,right,bottom);
    }
}

Then in verticalgridfragment class use

CustomVerticalGridPresenter videoGridPresenter = new 
CustomVerticalGridPresenter(ZOOM_FACTOR, false);

instead of

VerticalGridPresentervideoGridPresenter = new VerticalGridPresenter(ZOOM_FACTOR, false);
Filberte answered 12/12, 2018 at 6:32 Comment(0)
A
4

Another way over this problem is to set the windowAlignment attributes of the VerticalGridView. Most notably ...

verticalGridView.windowAlignment = BaseGridView.WINDOW_ALIGN_LOW_EDGE //this will remove the top margin
verticalGridView.windowAlignmentOffset = 0
verticalGridView.windowAlignmentOffsetPercent = 25f //find correct values for your case
Adjourn answered 18/2, 2019 at 8:20 Comment(0)
P
2

First of follow this steps to move the rows up which is by default given margins by Lean back Library.

1. Go to you SDK.

2. Inside SDK -> extras -> android -> support -> v17 -> leanback -> res -> values.

3. Inside values folder copy the dimens.xml inside your current project and copy to your projects res -> values folder.

Now you have the file name dimens.xml inside your values folder.

Now open the dimens.xml file which you have copied.

Change the value for property defined below. By default it will given 168dp around that. So make it less to decrease the top margin as i have given below.

<dimen name="lb_browse_rows_margin_top">35dp</dimen>

Now you can be able to split your rows up exact below of top section of your Browse Fragment.

Precast answered 19/8, 2016 at 7:16 Comment(6)
Thank you for the help, I am trying to do as explained, I am getting Resource not found error as following: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v17.leanback.widget.TitleViewAlacrity
@Alacrity Can you please edit your question and add this full Logcat information in above question and also mention what changes you made and raised this exception. Because this dimen file is not using any resources. you have something else mismatch.Precast
It's not, it should be using leanback layouts, I didn't make any changes, i just added the dimens file to res/values and I edited it to add my custom dimen values, the projects run then a runtime exception occurs. I can't copy logcat, it's too long to be fitted here Note: there a 2 similar errors (Error inflating class android.support.v17.leanback.widget.TitleView and SearchOrbView)Alacrity
@Alacrity Yes it should use Lean back layouts that's why i have mentioned that above exception is not because of dimen file but because of something else. dimen files are just using the values in dp for all layouts which are provided by leanback. And i have the same issue of displaying row from top i have solved it by changing only dp values in dimen file nothing else.Precast
this solution could help, but it didn't work with me, I found the right solution, please check my next answerAlacrity
it works for browse fragment not for verticalgrid fragmentFilberte
I
0

The best way is to create a theme and set it for your Activity / Fragment.

In themes.xml:

<style name="AppTheme.Leanback.NoTitle" parent="Theme.Leanback.Browse">
    <item name="browseRowsMarginTop">@dimen/lb_browse_rows_margin_top_none</item>>
</style>

in dimens.xml

<dimen name="lb_browse_rows_margin_top_none">32dp</dimen>
Ingold answered 27/6, 2022 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.