Skip to content Skip to sidebar Skip to footer

Using Viewtreeobserver I Am Adding Setmaxlines And Setellipsize In Materialtextview Inside Recyclerview.adapter

I am set MaterialTextView inside RelativeLayout and set RelativeLayout size programmatically different size for every device. Then i have using ViewTreeObserver to set setMaxLines

Solution 1:

I am not completely sure about the problem that you are having there. However, I think I found some problems in your code.

The first problem that I see in setting up your RecyclerView is setting the layout size fixed by the following.

recyclerView.setHasFixedSize(true);

Looks like you are changing the item layout size dynamically. Hence you need to remove the line above while setting up your RecyclerView.

The second problem that I see is, there is no textView_category in the ViewHolder for Quote. Hence the following should throw an error.

quotes.textView_category.setText(subCategoryLists.get(position).getCategory_name());

Solution 2:

One problem I can see is that you are calling

quotes.textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

in the first line of your listener callback no matter what. In a RecyclerView the first few times this is called the layout might still be unmeasured but you're cancelling the listener callback after the first callback. So this line fires, and if the view is still unmeasured then the next few lines are going to fail and your code will get no more opportunities to fill the view. Try checking the measuredWidth or measuredHeight of your view for something greater than 0 before cancelling future listener callbacks.

Solution 3:

I have using this. works perfectly

textView.setText(subCategoryLists.get(position).getStatus_title());
textView.post(new Runnable() {
    @Override
    publicvoidrun() {
        ViewGroup.LayoutParams params = textView.getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        final int widthSpec = View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        final int heightSpec = View.MeasureSpec.makeMeasureSpec(textView.getHeight(), View.MeasureSpec.UNSPECIFIED);
        textView.measure(widthSpec, heightSpec);
        textView.setMaxLines(heightSpec / textView.getLineHeight());
        textView.setEllipsize(TextUtils.TruncateAt.END);
    }
});

Post a Comment for "Using Viewtreeobserver I Am Adding Setmaxlines And Setellipsize In Materialtextview Inside Recyclerview.adapter"