Panich Maxim
Panich Maxim

Reputation: 1115

Android TV ImageCardView title wrapping

I am developing app for Android TV and using leanback library.

In example from google they are using public class CardPresenter extends Presenter to display content. The problem is, that I want to display all text in titles, i.e. dont cut it.

enter image description here

I already tried:

1) To fix this problem by programmatically setting LayoutParams: cardView.findViewById(R.id.title_text).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

2) I looked inside lb_image_card_view.xml file for ImageCard. Funny moment, that TextView with id "title_text" already has android:layout_height="wrap_content" parameter, but looks like it doesn't work?? Is it a bug?

3) For a backup plan I can create my own class for ImageCardView, but this solution seems 2 hard.

Thx.

Update.

I used the answer below, but I have to modificative code for better performance:

cardView.setOnFocusChangeListener((view, isFocused) -> {
        if (isFocused) {
            ((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(5);
        }
        else {
            ((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(1);
        }
    });

Upvotes: 3

Views: 2200

Answers (1)

KENdi
KENdi

Reputation: 7771

Try to check this tutorial if it can help you. it helps you to show or display all the text in title.

Here is the code use for this tutorial

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    final ImageCardView cardView = new ImageCardView(mContext);    

    cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, final boolean isFocused) {
            final View infoField = view.findViewById(R.id.info_field);
            final TextView contentField = (TextView)view.findViewById(R.id.content_text);
            final TextView titleField = (TextView)view.findViewById(R.id.title_text);
            final Drawable mainImage = ((ImageView)view.findViewById(R.id.main_image)).getDrawable();

            if (isFocused) {
                ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(3);
                FrameLayout.LayoutParams infoLayout = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                infoField.setLayoutParams(infoLayout);
                RelativeLayout.LayoutParams contentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                contentLayout.addRule(RelativeLayout.BELOW, R.id.title_text);
                contentField.setLayoutParams(contentLayout);
            }
            else {
                ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(1);
            }
        }
    });

    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
    return new ViewHolder(cardView);
}

And here is the output of this code.

enter image description here

For more information, check this thread also.

Upvotes: 5

Related Questions