TextView textView = findViewById(R.id.textView); textView.setLines(2);
SpannableString text = new SpannableString("This is a long text that needs to be truncated after 3 lines."); TextView textView = findViewById(R.id.textView); textView.setLines(3); textView.setText(text, TextView.BufferType.SPANNABLE);In this example, we create a SpannableString object that contains a long text. We set the maximum number of lines in the TextView to 3 and then set the text using the SpannableString object. If the text exceeds 3 lines, it will be truncated with an ellipsis. Package Library: android.widget.