Drilon Kurti
Drilon Kurti

Reputation: 399

Get how many characters a line in Textview can have

Let's say we have a large String and many textviews which all have maxLines="1" and layout_width="match_parent"and a monospaced font.

I need to know how many characters of my string I can put in a Textview before it needs a new line.

It would be great if the solution could work with EditText too.

Upvotes: 5

Views: 1923

Answers (1)

tiny sunlight
tiny sunlight

Reputation: 6251

TextPaint paint = textView.getPaint();
int wordwidth=(int)paint.measureText("a",0,1);
int screenwidth = getResources().getDisplayMetrics().widthPixels;
int num = screenwidth/wordwidth

Upvotes: 4

Related Questions