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

前端 未结 21 1931
野的像风
野的像风 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:32

    eg. you can use

    android:maxLength="13"
    

    this will restrict texview length to 13 but problem is if you try to add 3 dots(...), it wont display it, as it will be part of textview length.

         String userName;
         if (data.length() >= 13) {
                userName = data.substring(0, 13)+ "...";
    
         } else {
    
                userName = data;
    
        }
            textView.setText(userName);
    

    apart from this you have to use

     android:maxLines="1"
    

提交回复
热议问题