Android Implementation Expendabletextview Extensible TextView

Source: Internet
Author: User

Introduced

In the application development, always encounter some similar to the announcement, explain the textview of the long text, but in order to typography and other factors, we usually want to hide the second half of the text, only show some text, and then at the end of the user will be provided with an extension/contraction of the button, So that the text box can be expanded when needed to view the full text, which requires implementing a expendabletextview, similar to expendablelist.

Principle

1, start using Android:lines to set the number of TextView, click the button, remove the limit.

2. Use Android:ellipsize to set the omitted position of the text.

3, remember to set android:layout_height= "Wrap_content", or fixed a height, it can not be achieved.

Realize

Main.xml layout

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.appupdate.MainActivity" >    <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:ellipsize= "End"Android:lines= "3"Android:text= "I was stating that I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was the announcement I was stating I was the announcement I was yes, I'm an announcement, I'm an announcement, I'm a statement, I'm an announcement, I'm an announcement, I'm a statement, I'm an announcement, I'm a statement, I'm an announcement, I'm a statement, I'm an announcement, I'm saying Ming I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement "Android:textcolor= "@android: Color/black" />    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/textview1"Android:layout_centerhorizontal= "true"Android:text= "Click to expand" /></Relativelayout>

Core code

PrivateTextView TextView1; PrivateButton button1; Private BooleanIsexpend =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button1=(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(!isexpend) {                    //RecoveryTextview1.setminlines (0);                    Textview1.setmaxlines (Integer.max_value); Button1.settext ("Click to shrink"); Isexpend=true; } Else{textview1.setlines (3); Button1.settext ("Click to expand"); Isexpend=false;    }            }        }); }

It looks like this after it's run.

Find a time to refactor, make a custom control, achieve a true sense of expendabletextview

Android Implementation Expendabletextview Extensible TextView

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.