I have a TextView which firstly shows a small portion of a long text.
The user can press a \"see more\" button to expand the TextView and s
You can do something like this. It will work in any kind of view, whether a normal view, or a view inside ListView or RecyclerView:
In onCreate() or something similar, add:
// initialize integers
int collapsedHeight, expandedHeight;
// get collapsed height after TextView is drawn
textView.post(new Runnable() {
@Override
public void run() {
collapsedHeight = textView.getMeasuredHeight();
}
});
// view that will expand/collapse your TextView
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// number of max lines when collapsed
if (textView.getMaxLines() == 2) {
// expand
textView.setMaxLines(Integer.MAX_VALUE);
textView.measure(View.MeasureSpec.makeMeasureSpec(notifMessage.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED));
expandedHeight = textView.getMeasuredHeight();
ObjectAnimator animation = ObjectAnimator.ofInt(textView, "height", collapsedHeight, expandedHeight);
animation.setDuration(250).start();
} else {
// collapse
ObjectAnimator animation = ObjectAnimator.ofInt(textView, "height", expandedHeight, collapsedHeight);
animation.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
// number of max lines when collapsed
textView.setMaxLines(2);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animation.setDuration(250).start();
}
}
});
This will let you expand/collapse a TextView by clicking any view you want. (you can surely choose the TextView itself too)