I am developing an Android application. My app has news feed list like Facebook. I am setting readmore feature if the text of TextView is too long for each post. I am using ExpandableTextView for readmore feature. But it is ok.
Then problem is with getting line count of TextView. I need to know the line count after I set the text to TextView because I need to toggle the visibility of Read more button. But getLineCount is not working well.
In the bindViewHolder of Recycler adapter, I set the text to TextView like this:
viewHolder.tvTitle.setText(post.getTitle());
viewHolder.tvTitle.post(new Runnable() {
@Override
public void run() {
Log.i("LINE_COUNT_"+String.valueOf(position),String.valueOf(viewHolder.tvTitle.getLineCount()));
if(viewHolder.tvTitle.getLineCount()>=3)
{
viewHolder.tvReadmore.setVisibility(View.VISIBLE);
}
else{
viewHolder.tvReadmore.setVisibility(View.GONE);
}
}
});
As you can see, I am using runnable because getLineCount() of TextView always returns 0 if I do not use it. But when I run my code and see my app, getLineCount() is working only for first 2 or 3 items. Not working for the rest of the items. I log the line count in the code. This is what I get in logcat. See the screenshot below.
As you can see in the logcat, it is returning value only for the first 2 items. For the rest of items, it always returns 0. Actually, all TextViews in the list has text line at least one. Some have 4 or 5. But it is not working. The runnable does not work well in RecyclerView Adapter? How can I fix my code to get the correct line count of textview in adapter?