I am trying to make an expandable textview, it is working correctly, but the problem is after rotation the textview.getLinesCount() returns the number of charcters in the textview not the number of lines, so it doesn't work correctly after the rotation !
here is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info, container, false);
if (getArguments() != null) {
overView = getArguments().getString("overview");
}
overViewTv = view.findViewById(R.id.info_overview);
moreView = view.findViewById(R.id.overview_more);
overViewTv.setText(overView);
int maxLines = 3;
overViewTv.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
overViewTv.getViewTreeObserver().removeOnPreDrawListener(this);
if (!moreClicked) {
if (overViewTv.getLineCount() > maxLines) {
moreView.setVisibility(View.VISIBLE);
overViewTv.setMaxLines(maxLines);
moreClicked = true;
}
}
return true;
}
});
moreView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (moreClicked) {
moreView.setImageResource(R.drawable.ic_expand_less_black_24dp);
overViewTv.setMaxLines(overViewTv.getLineCount());
moreClicked = false;
} else {
moreView.setImageResource(R.drawable.ic_expand_more_black_24dp);
overViewTv.setMaxLines(maxLines);
moreClicked = true;
}
}
});
return view;
}
I also tried this method getViewTreeObserver().addOnGlobalLayoutListener()
, but same thing happened.