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
Here is a repo with a similar approach: https://github.com/CorradiSebastian/ExpandableTextView
It came out from this question:
Custom Expandable TextView
Now, it's even more easy to provide the requested TextView with animation and all the required controls using this awesome library ExpandableTextView, in this library you have only to add it into your gradle and then define it like the following in your xml:
<com.ms.square.android.expandabletextview.ExpandableTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
android:id="@+id/expand_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
expandableTextView:maxCollapsedLines="4"
expandableTextView:animDuration="200">
<TextView
android:id="@id/expandable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#666666" />
<ImageButton
android:id="@id/expand_collapse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="right|bottom"
android:background="@android:color/transparent"/>
</com.ms.square.android.expandabletextview.ExpandableTextView>
and after that use it in your code like:
TextView expandableTextView = (ExpandableTextView) findViewById(R.id.expand_text_view);
And as you see you can control the max lines you want and the animation duration and all the required settings for your TextView expand technique.
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)