Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add "View More" at the end of TextView after 3 lines [duplicate]

I want to add "More" functionality after three lines of text. The text contains the description which is more than 10 lines. so we have decided to add "More" after three lines of text. Like:

enter image description here

when the text is showing the complete description, then it should show "Less" button at the end of text which again compress the TextView.

like image 577
Kanika Avatar asked Oct 30 '13 06:10

Kanika


People also ask

How do you check if a TextView's text exceeds the max lines?

text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

How do you add multiple lines in TextView?

When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines. When you set the layout_width and layout_height as wrap_content or match_parent , then the TextView widget will use all the available space to display the text you specified as its content.


1 Answers

Try this may help you working fine with me.

    public class MainActivity extends Activity {      TextView tv;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         tv = (TextView) findViewById(R.id.tv);         makeTextViewResizable(tv, 3, "View More", true);     }      @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.main, menu);          return true;     }      public static void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText, final boolean viewMore) {          if (tv.getTag() == null) {             tv.setTag(tv.getText());         }         ViewTreeObserver vto = tv.getViewTreeObserver();         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {              @SuppressWarnings("deprecation")             @Override             public void onGlobalLayout() {                 String text;                 int lineEndIndex;                 ViewTreeObserver obs = tv.getViewTreeObserver();                 obs.removeOnGlobalLayoutListener(this);                  if (maxLine == 0) {                     lineEndIndex = tv.getLayout().getLineEnd(0);                     text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;                 } else if (maxLine > 0 && tv.getLineCount() >= maxLine) {                     lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);                     text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;                 } else {                     lineEndIndex = tv.getLayout().getLineEnd(tv.getLayout().getLineCount() - 1);                     text = tv.getText().subSequence(0, lineEndIndex) + " " + expandText;                 }                 tv.setText(text);                 tv.setMovementMethod(LinkMovementMethod.getInstance());                 tv.setText(                             addClickablePartTextViewResizable(SpannableString(tv.getText().toString()), tv, lineEndIndex, expandText,                                     viewMore), BufferType.SPANNABLE);             }         });     }      private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv,             final int maxLine, final String spanableText, final boolean viewMore) {         String str = strSpanned.toString();         SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned);          if (str.contains(spanableText)) {             ssb.setSpan(new ClickableSpan() {                  @Override                 public void onClick(View widget) {                         tv.setLayoutParams(tv.getLayoutParams());                         tv.setText(tv.getTag().toString(), BufferType.SPANNABLE);                         tv.invalidate();                     if (viewMore) {                         makeTextViewResizable(tv, -1, "View Less", false);                     } else {                         makeTextViewResizable(tv, 3, "View More", true);                     }                  }             }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0);          }         return ssb;     } } 

UPDATE : Remove UnderLine from spaneble text

  1. Create Custom ClickableSpan
public class MySpannable extends ClickableSpan {      private boolean isUnderline = false;      /**      * Constructor      */     public MySpannable(boolean isUnderline) {         this.isUnderline = isUnderline;     }      @Override     public void updateDrawState(TextPaint ds) {         ds.setUnderlineText(isUnderline);         ds.setColor(Color.parseColor("#343434"));      }      @Override     public void onClick(View widget) {      } } 
  1. Change in addClickablePartTextViewResizable() method
private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv,                                                                             final int maxLine, final String spanableText, final boolean viewMore) {         String str = strSpanned.toString();         SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned);          if (str.contains(spanableText)) {             ssb.setSpan(new MySpannable(false){                 @Override                 public void onClick(View widget) {                         tv.setLayoutParams(tv.getLayoutParams());                         tv.setText(tv.getTag().toString(), BufferType.SPANNABLE);                         tv.invalidate();                     if (viewMore) {                         makeTextViewResizable(tv, -1, "View Less", false);                     } else {                         makeTextViewResizable(tv, 3, "View More", true);                     }                 }             }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0);          }         return ssb;     } 

OutPut:

enter image description here

enter image description here

like image 126
Biraj Zalavadia Avatar answered Sep 20 '22 17:09

Biraj Zalavadia