How to auto size textview dynamically according to the length of the text in android?

前端 未结 7 789
不思量自难忘°
不思量自难忘° 2021-02-04 07:07

I am taking data of variable sizes from the server and setting to a textView and i wanted the textview to resize according to the length of the text set.

It is given in

7条回答
  •  清酒与你
    2021-02-04 07:29

    Maybe you can try something like this:

    EditText et = someEditText;
    String text = et.getText().toString();
    int textLength = text.length();
    
    if (textLength > 25) {
        et.setTextSize(toSomeNumber as the size of the text);
    } else if (textLength > 15) {
         et.setTextSize(toSomeNumber bigger than the previous);
    } else {
          et.setTextSize(toSomeNumber this is the biggest);
    }
    

    UPDATE : You can refer to this question if you want to solve your problem in more android way. How to set text size of textview dynamically for different screens

    This is what I use for my own codes. Any suggestion is accepted to enhance my code. Hope it helps.

提交回复
热议问题