Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to expand TextView?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 360
    Comment on it

    Hi Friends,

    This blog will help you to expand TextView if textview has more than given line. Suppose you want visible only four line first time in TextView while your message length is greater than four line. In this case, you can give an another option button or textview ShowMore. After clicking on this button or textview your TextView will expand and will show complete message string.

    Step-1 XML file

    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/text_msg"
        android:textColor="#000000"
        android:textSize="25dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_show_more"
        android:textColor="#000000"
        android:gravity="right"
        android:layout_marginTop="8dp"
        android:text="Show More"
        android:textSize="25dp"/>
    
    

     

    Step-2 Create an activity
     

    public class ExpandTextView extends AppCompatActivity {
        private boolean isExpanded;
        private TextView mMessageTextView,mShowMoreTextView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text_demo);
    
            mMessageTextView=(TextView)findViewById(R.id.text_msg);
            mShowMoreTextView=(TextView)findViewById(R.id.text_show_more);
            mMessageTextView.setText("Test line 1"+"Test line 2"+"Test line 3"+"Test line 4 "+"Test line 5 "+"Test line 6 "+
            "Test line 7 "+"Test line 8");
    
            mMessageTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (!isExpanded) {
                        if (mMessageTextView.getLineCount() > 2) {
                            isExpanded = false;
                            mShowMoreTextView.setVisibility(View.VISIBLE);
                            mMessageTextView.setMaxLines(2);
                        } else {
                            isExpanded = false;
                        }
                    }
                }
            });
    
            mShowMoreTextView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (!isExpanded) {
                        isExpanded = true;
                        mMessageTextView.setMaxLines(800);
                        mShowMoreTextView.setText("Show Less");
                    } else {
                        isExpanded = false;
                        mMessageTextView.setMaxLines(4);
                        mShowMoreTextView.setText("Show More");
                    }
                }
            });
    
        }
    }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: