Developing an application for Android TV, I faced a problem for the usage of BrowseFragment provided by Leanback support library. Can we change the margin between items of RowsFragment?
-
Is padding the elements a solution? – Sebastiano Sep 04 '15 at 17:22
-
Thank you for fast reply, I could found a solution after all. – corochann Sep 07 '15 at 14:14
5 Answers
I found the answer by myself. The key was HorizontalGridView which stores the each rows item list. We can get the reference of this HorizontalGridView by R.id.row_content. Finally, setItemMargin method was the solution for this. Below is a sample code, and I could get top image.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
HorizontalGridView horizontalGridView = (HorizontalGridView) parent.findViewById(R.id.row_content);
horizontalGridView.setItemMargin(400); // You can set item margin here.
...
};

- 1,604
- 1
- 13
- 24
-
how can i set vertical margin for the list rows. could u pls help me with this. – Jay Rathod Feb 18 '16 at 10:40
-
1Try like `((ViewGroup.MarginLayoutParams) horizontalGridView.getLayoutParams()).setMargins(0, 0, 0, 100);` You can change the margin of horizontalGridView itself. – corochann Feb 19 '16 at 02:36
-
ok i will try. and also can u tell me how can i set up white border to card displayed in rows ? – Jay Rathod Feb 19 '16 at 05:57
-
1
-
1will you not mark your own answer as the correct? It could help lot of others searching – A_rmas Jun 10 '19 at 09:05
You can override some styles to achieve this, for example:
<style name="AppTheme.Leanback" parent="Theme.Leanback">
..
<item name="rowHorizontalGridStyle">@style/TvHorizontalGridView</item>
..
</style>
<style name="TvHorizontalGridView" parent="Widget.Leanback.Row.HorizontalGridView">
<item name="horizontalMargin">@dimen/margin_medium</item>
<item name="verticalMargin">@dimen/margin_medium</item>
</style>
where @dimen/margin_medium
is the size of the margin that you want.

- 1,374
- 1
- 17
- 25
Ideally override the style details as @Billy answered. It can unfortunately take a while to figure out whats what. If what you are looking for is not styleable you can apparently override the built-in leanback resources (not sure if reliable/safe though). eg:
<dimen name="lb_browse_rows_margin_top">167dp</dimen>
<dimen name="lb_browse_item_vertical_spacing">8dp</dimen>
<dimen name="lb_browse_expanded_row_no_hovercard_bottom_padding">28dp</dimen>

- 381
- 1
- 14
-
I suggest to create different styles for these instead of overriding the default resources as they are reused a lot. – A_rmas Jun 20 '19 at 05:05
I had a case when there was a need to implement an endless scroll in ListRow. Also, I needed to add extra space after each last item. So I solved this issue by adding custom presenter to adapter's presenterSelector:
class EmptySpacePresenter : Presenter() {
override fun onCreateViewHolder(parent: ViewGroup): ViewHolder {
val emptySpaceView = FrameLayout(parent.context).apply {
val width = context.resources.getDimension(R.dimen.gallery_carousel_horizontal_gap).toInt()
layoutParams = FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.MATCH_PARENT)
isClickable = false
isFocusable = false
isFocusableInTouchMode = false
}
return ViewHolder(emptySpaceView)
}
override fun onBindViewHolder(viewHolder: ViewHolder?, item: Any?) = Unit
override fun onUnbindViewHolder(viewHolder: ViewHolder?) = Unit
}
class MyObjectAdapter() : ArrayObjectAdapter() {
init {
presenterSelector = ClassPresenterSelector()
.addClassPresenter(ListItem::class.java, ListItemPresenter())
.addClassPresenter(EmptySpace::class.java, EmptySpacePresenter())
}
}

- 107
- 1
- 9
-
EmptySpace.class contains what? I have not found anything in leanback libarary. – kalpana c Sep 21 '21 at 17:21
In Presenter class onCreateViewHolder method,we can set parameters using setItemSpacing,setPadding e.t.c
class TVPresenter() : Presenter() {
override fun onCreateViewHolder(parent: ViewGroup): ViewHolder {
parent.findViewById<HorizontalGridView>(R.id.row_content).run {
setItemSpacing(10)
setPadding(40,10,40,10)
}
val cardViewLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_layout, parent, false)
return ViewHolder(cardViewLayout)
}

- 2,232
- 16
- 15