Reputation: 910
I have developed a tv application using leanback library and I want to add a white border to the selected item along with zoom out effect, zoom is happening but there is no method to add border. I have searched a lot but could not found any possible work around.
public class CardPresenter extends Presenter {
private int mSelectedBackgroundColor = -1;
private int mDefaultBackgroundColor = -1;
private Drawable mDefaultCardImage;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
mDefaultBackgroundColor =
ContextCompat.getColor(parent.getContext(), R.color.default_background);
mSelectedBackgroundColor =
ContextCompat.getColor(parent.getContext(), R.color.selected_background);
ImageCardView cardView = new ImageCardView(parent.getContext()) {
@Override
public void setSelected(boolean selected) {
updateCardBackgroundColor(this, selected);
super.setSelected(selected);
}
};
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
updateCardBackgroundColor(cardView, false);
return new ViewHolder(cardView);
}
private void updateCardBackgroundColor(ImageCardView view, boolean selected) {
int color = selected ? mSelectedBackgroundColor : mDefaultBackgroundColor;
view.setBackgroundColor(color);
view.findViewById(R.id.info_field).setBackgroundColor(color);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
VideoItem video = (VideoItem) item;
ImageCardView cardView = (ImageCardView) viewHolder.view;
cardView.setTitleText(video.getVideoName());
if (video.getVideoImageThumbnail() != null) {
// Set card size from dimension resources.
Resources res = cardView.getResources();
int width = res.getDimensionPixelSize(R.dimen.card_width);
int height = res.getDimensionPixelSize(R.dimen.card_height);
cardView.setMainImageDimensions(width, height);
Glide.with(cardView.getContext())
.load(video.getVideoImageThumbnail())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(cardView.getMainImageView());
}
}
}
//Load rows method
private void loadRows() {
// to make item look large
ListRowPresenter presenter= new
ListRowPresenter(FocusHighlight.ZOOM_FACTOR_LARGE,true);
presenter.setShadowEnabled(false);
mRowsAdapter = new ArrayObjectAdapter(presenter);
}
Upvotes: 0
Views: 1249
Reputation: 796
Leanback's CardView
does not support adding a border. You'll have to use Material Components' MaterialCardView
for that.
First, add the Material Components library in your project:
implementation "com.google.android.material:material:1.1.0-alpha10"
Then, make your ImageCardView
extend MaterialCardView
instead of BaseCardView
, and add the following code in it:
init {
borderWidth = 0f
setBorderColor(Color.WHITE)
}
override fun setSelected(selected: Boolean) {
super.setSelected(selected)
borderWidth = if (selected) 4f else 0f
}
Upvotes: 2