0

I've large string, i want to split it. i got screen width and height using below code,

DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;

I want to know how many character to display on screen.

how to calculate ? and split the string.?

RVG
  • 3,498
  • 4
  • 36
  • 63

3 Answers3

1

On a Swing / AWT Java platform, you could use a FontMetrics object to measure the width of the particular characters you are trying to display.

References:

But it would probably be simpler to use something that can take care of the character rendering and wrapping for you.

On the Android platform, the Paint class has a number of methods that will help you do this kind of thing.

Community
  • 1
  • 1
Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
0

use the substring method

String substring(int startIndex, int endIndex)

split the strings per line and store them in an array of Strings

String originalString=" ....some long text " ;
String stringsByLine[]= new String(screenHeight);
int i,j=0;
for ( i = 0; j<originalString.length;i++,j++){
  stringsByLine[i]=originalString.substring(j,j+screenWidth);
  j+=screenWidth;
}

havent tried it myself, but this logic should work. :-)

Vinay W
  • 9,583
  • 7
  • 36
  • 47
0

i think if you set width property to wrapcontent than string automatically split and display on next line.

use this property

android:layout_width="wrap_content"
            android:layout_height="wrap_content"
Sanket Kachhela
  • 10,698
  • 7
  • 48
  • 75