how can I, character and words count from string to make a line break insert in android


Keywords:android 


Question: 

how can I, character and words count from string to make a line break insert in android

I have a text output of words with max 150 letters and want the all 50 letters carried a line break without separating the word. how can I implement it? here is a code snippet ....

ll = (TableLayout) rootview.findViewById(R.id.displayLinear);
TableRow row = new TableRow(getActivity());
consoleLocalStore.storeConsoleData(consolex);

LayoutInflater inflater = getLayoutInflater(getArguments());
View theInflatedView = inflater.inflate(R.layout.activity_consolex,
        null);

TextView attid = (TextView) theInflatedView.findViewById(R.id.output);

attid.setText(String.valueOf(consolex.output));
attid = new TextView(getActivity());
row.addView(attid);

row.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(theInflatedView);


ll.addView(row, i);

1 Answer: 

Say you want to insert a \n at index closer to 50. You could probably use StringBuilder :

 StringBuilder builder = new StringBuilder(yourString);
 boolean inserted;
 int index = 50;

 do{

      if(yourString.charAt(index)==' '){
               builder.insert(index,'\n');
                inserted=true;
        }
       index++;
        /* or index-- if you want to insert \n before 50th character*/
       }while(inserted == false);

Hope this proves helpful.