Android, How to limit width of TextView (and add three dots at the end of text)?

前端 未结 21 1829
野的像风
野的像风 2020-12-02 03:44

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I\'m looking for is how to add three dots (...) at the end o

相关标签:
21条回答
  • 2020-12-02 04:41

    You can limit your textview's number of characters and add (...) after the text. Suppose You need to show 5 letters only and thereafter you need to show (...), Just do the following :

    String YourString = "abcdefghijk";
    
    if(YourString.length()>5){
    
    YourString  =  YourString.substring(0,4)+"...";
    
    your_text_view.setText(YourString);
    }else{
    
     your_text_view.setText(YourString); //Dont do any change
    
    }
    

    a little hack ^_^. Though its not a good solution. But a work around which worked for me :D

    EDIT: I have added check for less character as per your limited no. of characters.

    0 讨论(0)
  • 2020-12-02 04:41
            <TextView
                android:id="@+id/product_description"
                android:layout_width="165dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                android:text="Pack of 4 summer printed pajama"
                android:textColor="#d2131c"
                android:textSize="12sp"
                android:maxLines="2"
                android:ellipsize="end"/>
    
    0 讨论(0)
  • 2020-12-02 04:41

    Add These two lines in your text

    android:ellipsize="end"
    android:singleLine="true"
    
    0 讨论(0)
提交回复
热议问题