How to control when TextView sends a String to a new line?

When a word in a String from TextView too large to fit on the same line as the previous words, it moves to the next line. It is very useful. However, this puts me in a dilemma. My String , for my special reasons, should have a space between each letter of the word and two spaces between the words. Because of this, the TextView will split words that have one part on one line and another part on the next. So, I think that I may have to create a custom View that extends the TextView and redefines the way to go to the next line. But looking at class docs for text viewing , I can't find a way to do this. So, any help, advice or suggestions will be greatly appreciated! Thanks!

+3
java android string textview
source share
1 answer

You can override TextView few changes.

The main strategy will be to override the onDraw() method and custom paint your text. You will also have to override onMeasure() .

Inside onDraw() you will pre-process the custom words in your line and insert newline characters ( \n ) as necessary.

Scroll through the words in the text and compare measureText (String) with the current width of your TextView to determine where to insert line feeds. Do not forget to consider the "indentation".

An example :

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(getSuggestedMinimumWidth(), getSuggestedMinimumHeight()); } @Override protected void onDraw(Canvas canvas) { // preprocess your text canvas.drawText(...); } 
+1
source share

All Articles