From e19dee729377dee54575170eb30d087a513ba7b4 Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 18 Jul 2017 21:18:15 +0500 Subject: [PATCH] Fix setting max lines via code --- .../at/blogc/android/activities/MainActivity.java | 3 +++ .../blogc/android/views/ExpandableTextView.java | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/at/blogc/android/activities/MainActivity.java b/app/src/main/java/at/blogc/android/activities/MainActivity.java index 4f4cbc4..3df1236 100644 --- a/app/src/main/java/at/blogc/android/activities/MainActivity.java +++ b/app/src/main/java/at/blogc/android/activities/MainActivity.java @@ -39,6 +39,9 @@ protected void onCreate(final Bundle savedInstanceState) final ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView); final Button buttonToggle = (Button) this.findViewById(R.id.button_toggle); + // set max lines via code + expandableTextView.setMaxLines(3); + // set animation duration via code, but preferable in your layout files by using the animation_duration attribute expandableTextView.setAnimationDuration(750L); diff --git a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java index f53d230..83b0f58 100644 --- a/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java +++ b/expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java @@ -43,7 +43,7 @@ public class ExpandableTextView extends TextView private TimeInterpolator expandInterpolator; private TimeInterpolator collapseInterpolator; - private final int maxLines; + private int maxLines; private long animationDuration; private boolean animating; private boolean expanded; @@ -101,7 +101,14 @@ public int getMaxLines() return -1; } } - + + @Override + public void setMaxLines(int maxLines) + { + super.setMaxLines(maxLines); + this.maxLines = maxLines; + } + /** * Toggle the expanded state of this {@link ExpandableTextView}. * @return true if toggled, false otherwise. @@ -139,7 +146,7 @@ public boolean expand() this.collapsedHeight = this.getMeasuredHeight(); // set maxLines to MAX Integer, so we can calculate the expanded height - this.setMaxLines(Integer.MAX_VALUE); + super.setMaxLines(Integer.MAX_VALUE); // get expanded height this.measure @@ -232,7 +239,7 @@ public void onAnimationUpdate(final ValueAnimator animation) public void onAnimationEnd(final Animator animation) { // set maxLines to original value - ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines); + ExpandableTextView.super.setMaxLines(ExpandableTextView.this.maxLines); // if fully collapsed, set height to WRAP_CONTENT, because when rotating the device // the height calculated with this ValueAnimator isn't correct anymore