Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to expand only textview in android

i am trying to expand textview in android, i have textview with some bulk content, my requirement is to display only two lines from bulk content with finishing (...). if i click it, my full content will has to display. how to achieve this. could you please help me?

public class MainActivity extends ActionBarActivity {       
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);  
   t1 = (TextView) findViewById(R.id.text);
   String str = "If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled." +
                "I made the expandable list view acc to your tutorial but what i have to do if I want to populate Expandable List View with values from database?Means what changes I have to do in ";
    t1.setText(str);                               
    t1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        t1.setMaxLines(Integer.MAX_VALUE);
    }
 });
}
}
like image 519
dawood rizwan Avatar asked Dec 14 '22 17:12

dawood rizwan


2 Answers

You can set android:maxLines property of textview to 2 by default in your layout xml..

In your onCreate() function after setting the text to your textview (t1).
Add following

 boolean isTextViewClicked = false;

t1.setOnClickListener(new OnClickListener() {
    if(isTextViewClicked){
      //This will shrink textview to 2 lines if it is expanded.
       t1.setmaxLines(2);
       isTextViewClicked = false;
    } else {
       //This will expand the textview if it is of 2 lines
       t1.setMaxLines(Integer.MAX_VALUE);
       isTextViewClicked = true;
    }
});

This will expand the textview as per the content

like image 159
Ichigo Kurosaki Avatar answered Dec 29 '22 19:12

Ichigo Kurosaki


I describe the best and optimize approach in a medium article.

enter image description here

What you need to do is running the following function in a TextView.post{} function:

private fun TextView.setCaption(senderName: String, caption: String) {
    text = getFullCaption(senderName, caption)

    if (lineCount > DEFAULT_LINES) {
        val lastCharShown = layout.getLineVisibleEnd(DEFAULT_LINES - 1)
        maxLines = DEFAULT_LINES
        val moreString = context.getString(R.string.read_more)
        val suffix = " $moreString"
        // 3 is a "magic number" but it's just basically the length of the ellipsis we're going to insert
        val actionDisplayText = context.getString(R.string.more_dots) + suffix

        text = SpannableStringBuilder()
            .bold { append(senderName) }
            .append("  ")
            .append(
                caption.substring(
                    0,
                    lastCharShown - suffix.length - 3 - moreString.length - senderName.length
                )
            )
            .color(Color.GRAY) { append(actionDisplayText) }
    }
}

Calling setCapton function in an extensive function of TextView and set click listener for it to expand, is your final step to have an expandable TextView:

private fun TextView.setExpandableText(senderName: String, caption: String) {
    post {
        setCaption(senderName, caption)
        setOnClickListener {
            let {
                it.maxLines = MAX_LINES
                it.text = getFullCaption(senderName, caption)
            }
        }
    }
}
like image 27
AliSh Avatar answered Dec 29 '22 18:12

AliSh