Search code examples
androidtextview

Android - Expand ellipsized TextView


I'm trying to implement a function that when a TextView contains too many lines of text, it can be collapsed into 2 lines after a user taps a button. It is expected that when the user taps that button again, the TextView should then show the full text it contains. In the collapseView() method, I use setEllipsize as below:

private void collapseView() {
        m_vwText.setMaxLines(2);
        m_vwText.setEllipsize(TruncateAt.END);
        m_vwExpandButton.setText(EXPAND);
    } 

So I wonder what should I do in my expandView() method to show all lines contained in the TextView. Please help and thanks in advance.


Solution

  • An idea would be to take the original TextView value into a temporary variable and keep it there, and make use of it in the customView() method.

    ex:, in onCreate()

    String originalText=m_vwText.getText().toString().
    

    Then in your expandView() you just work with originalText

    private void expandView(){
       m_vwText.setText(originalText);
    }