Manikanta Manikanta - 1 year ago 143
Android Question

Android Expandable text view with "View More" Button displaying at center after 3 lines

I want to display description in a text view, if the description is more than 3 lines i want to display a View more button that expands the text view with complete description (or a dialog with complete description)

I have referred some links

1) How can I implement a collapsible view like the one from Google Play?

The above solution has an arrow like image view at end of third line of text view but i want a button displaying below the text view if description crosses 3 lines.

2) Add "View More" at the end of textview after 3 lines

The above solution has "View More" unaligned( "View" at 3rd line and "More" at 4th line)

I am looking for something like this.
enter image description here

Answer Source

This is how i have achieved the desired output,

MytextView which i want to expand is: tvDescription I have a see more button with name: btnSeeMore

To check if the textview has more than 4 lines i had a listener for it as follows,

tvDescription.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    if(expandable) {
                        expandable = false;
                        if (tvDescription.getLineCount() > 4) {
                            ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);

I have kept a boolean value to check if textview is already expanded so that there will not be any hickups while collapsing it.

if textview has more than four lines, boolean flag will be true and the button will be visible so this is the code for expanding and collapsing animation.

btnSeeMore.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (!expand) {
                    expand = true;
                    ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 40);
                    btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_collapse));
                } else {
                    expand = false;
                    ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);


Comment below for further information

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download