android TextView multi line text of more than 3 lines using the ellipsize attribute invalid solution

  • 2020-05-19 05:42:56
  • OfStack

TextView properties in the layout file


<TextView
android:id="@+id/businesscardsingle_content_abstract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.0"
android:lines="6"
android:text="@string/agrinbusiness_content"
android:textColor="#7f7f7f"
android:textSize="13sp" />

Controls the number of lines of text displayed in the JAVA code


ViewTreeObserver observer = textAbstract.getViewTreeObserver(); //textAbstract for TextView controls 
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = textAbstract.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if(textAbstract.getLineCount() > 6) // Determines how many rows are greater than 
  {
    int lineEndIndex = textAbstract.getLayout().getLineEnd(5); // Set up the first 6 Ellipsis the lines 
    String text = textAbstract.getText().subSequence(0, lineEndIndex-3) +"...";
    textAbstract.setText(text);
  }
  }
});


Related articles: