How do I tell if my textview has been ellipsized?

前端 未结 14 1632
天涯浪人
天涯浪人 2020-11-30 19:31

I have a multi-line TextView that has android:ellipsize=\"end\" set. I would like to know, however, if the string I place in there is actually too

相关标签:
14条回答
  • 2020-11-30 19:48

    create a method inside your TextViewUtils class

    public static boolean isEllipsized(String newValue, String oldValue) {
        return !((newValue).equals(oldValue));
    }
    

    call this method when it's required eg:

            if (TextViewUtils.isEllipsized(textviewDescription.getLayout().getText().toString(), yourModelObject.getDescription()))
                holder.viewMore.setVisibility(View.VISIBLE);//show view more option
            else
                holder.viewMore.setVisibility(View.GONE);//hide 
    

    but textView.getLayout() can't call before the view(layout) set.

    0 讨论(0)
  • 2020-11-30 19:49

    You can get the layout of the TextView and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:

    Layout l = textview.getLayout();
    if (l != null) {
        int lines = l.getLineCount();
        if (lines > 0)
            if (l.getEllipsisCount(lines-1) > 0)
                Log.d(TAG, "Text is ellipsized");
    }
    

    This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.

    0 讨论(0)
  • 2020-11-30 19:51

    In Kotlin, you can use the below code.

    var str= "Kotlin is one of the best languages."
    textView.text=str
    textView.post {
    val isEllipsize: Boolean = !textView.layout.text.toString().equals(str)
    
    if (isEllipsize) {
         holder.itemView.tv_viewMore.visibility = View.VISIBLE
    } else {
         holder.itemView.tv_viewMore.visibility = View.GONE
    }    
    }
    
    0 讨论(0)
  • 2020-11-30 19:54

    Simple Kotlin method. Allows android:ellipsize and android:maxLines to be used

    fun isEllipsized(textView: TextView, text: String?) = textView.layout.text.toString() != text
    
    0 讨论(0)
  • 2020-11-30 19:57

    public int getEllipsisCount (int line):

    Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.

    So, simply call :

    int lineCount = textview1.getLineCount();
    
    if(textview1.getLayout().getEllipsisCount(lineCount) > 0) {
       // Do anything here..
    }
    

    Since the getLayout() cant be called before the layout is set, use this:

    ViewTreeObserver vto = textview.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
           Layout l = textview.getLayout();
           if ( l != null){
              int lines = l.getLineCount();
              if ( lines > 0)
                  if ( l.getEllipsisCount(lines-1) > 0)
                    Log.d(TAG, "Text is ellipsized");
           }  
        }
    });
    

    And finally do not forget to remove removeOnGlobalLayoutListener when you need it nomore.

    0 讨论(0)
  • 2020-11-30 20:00

    Really work so, for example, to pass full data to dialog from item of RecyclerView:

    holder.subInfo.post(new Runnable() {
                    @Override
                    public void run() {
                        Layout l = holder.subInfo.getLayout();
                        if (l != null) {
                            final int count = l.getLineCount();
                            if (count >= 3) {
                                holder.subInfo.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        final int c = holder.subInfo.getLineCount();
                                        if (c >= 3) {
                                            onClickToShowInfoDialog.showDialog(holder.title.getText().toString(), holder.subInfo.getText().toString());
                                        }
                                    }
                                });
                            }
                        }
                    }
                });
    
    0 讨论(0)
提交回复
热议问题