Search code examples
androidandroid-tvleanback

How to change the background color of the info area on a ImageCardView?


I am trying to change the background color of the infoArea on an ImageCardView in the Android Leanback Library when the card is selected. Currently what I have tried is changing the background in the OnItemViewSelectedListener. This changes the background, but then doesn't clear the previously selected item.

private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
            if (item instanceof Video) {
                mBackgroundURI = ((Video) item).getBackgroundImageURI();
                startBackgroundTimer();
                ((ImageCardView) itemViewHolder.view)
                        .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey));
            }
        }
    }

I would like to achieve something like this:

desired effect

Any ideas? Thanks.


Solution

  • I found a simpler solution where I just keep track of the view that is currently selected and then change the background area based on that.

    private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
    
            private ImageCardView currentlySelectedView = null;
    
            @Override
            public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                       RowPresenter.ViewHolder rowViewHolder, Row row) {
                if (item instanceof Video) {
                    mBackgroundURI = ((Video) item).getBackgroundImageURI();
                    startBackgroundTimer();
    
                    if (currentlySelectedView != null) {
                        currentlySelectedView.setInfoAreaBackgroundColor(
                                getResources().getColor(R.color.lb_basic_card_info_bg_color));
                    }
    
                    currentlySelectedView = (ImageCardView) itemViewHolder.view;
                    currentlySelectedView
                            .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey));
                }
            }
        }