Android textView collapse and expand ExpandableTextView

Source: Internet
Author: User

TextView that can be folded and expanded may be used in the project process. Here we provide an implementation idea, custom control.

package com.example.expandtextviewdemo;import android.content.Context;import android.content.res.TypedArray;import android.text.SpannableStringBuilder;import android.util.AttributeSet;import android.view.View;import android.widget.TextView;public class ExpandableTextView extends TextView {    private static final int DEFAULT_TRIM_LENGTH = 200;    private static final String ELLIPSIS = ".....";     private CharSequence originalText;    private CharSequence trimmedText;    private BufferType bufferType;    private boolean trim = true;    private int trimLength;     public ExpandableTextView(Context context) {        this(context, null);    }     public ExpandableTextView(Context context, AttributeSet attrs) {        super(context, attrs);         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);        typedArray.recycle();         setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                trim = !trim;                setText();                requestFocusFromTouch();            }        });    }     private void setText() {        super.setText(getDisplayableText(), bufferType);    }     private CharSequence getDisplayableText() {        return trim ? trimmedText : originalText;    }     @Override    public void setText(CharSequence text, BufferType type) {        originalText = text;        trimmedText = getTrimmedText(text);        bufferType = type;        setText();    }     private CharSequence getTrimmedText(CharSequence text) {        if (originalText != null && originalText.length() > trimLength) {            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);        } else {            return originalText;        }    }     public CharSequence getOriginalText() {        return originalText;    }     public void setTrimLength(int trimLength) {        this.trimLength = trimLength;        trimmedText = getTrimmedText(originalText);        setText();    }     public int getTrimLength() {        return trimLength;    }}

Add the following content to your attr. xml file:

 
     
              
  
 

Then reference the following code in your layout file:

     
  
 

Finally, test it in your acitivty.


package com.example.expandtextviewdemo;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class ExpandableTextActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_expandable_text);String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "+ "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae "+ "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec "+ "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam "+ "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel "+ "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla "+ "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam"+ " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante"+ " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem "+ "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum"+ " sed laoreet suscipit. ";ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum);expandableTextView.setText(yourText);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.expandable_text, menu);return true;}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.