I am in the process of developing an e-book reader application for android 3.0 tablets. To begin with I have a large chunk of String data. I want to split/break that String into pages based on the screen size of the device [I am planning to use text switcher or view flipper]. Though I tried using getWindowManager() method I could not get the preferred results.
In the following thread it is mentioned that Text Switcher automatically breaks the text according to the screen size. But I do not think so. Managing text in android applicaiton like in a eBook
This is the logic I used :
// retreiving the flipper
flipper = (ViewFlipper) findViewById(R.id.new_view_flipper);
// obtaining screen dimensions
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
// contentString is the whole string of the book
while (contentString != null && contentString.length() != 0)
{
totalPages ++;
// creating new textviews for every page
TextView contentTextView = new TextView(this);
contentTextView.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
contentTextView.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
contentTextView.setMaxHeight(screenHeight);
contentTextView.setMaxWidth(screenWidth);
float textSize = contentTextView.getTextSize();
Paint paint = new Paint();
paint.setTextSize(textSize);
int numChars = 0;
int lineCount = 0;
int maxLineCount = screenHeight/contentTextView.getLineHeight();
contentTextView.setLines(maxLineCount);
while ((lineCount < maxLineCount) && (numChars < contentString.length())) {
numChars = numChars + paint.breakText(contentString.substring(numChars), true, screenWidth, null);
lineCount ++;
}
// retrieve the String to be displayed in the current textbox
String toBeDisplayed = contentString.substring(0, numChars);
contentString = contentString.substring(numChars);
contentTextView.setText(toBeDisplayed);
flipper.addView(contentTextView);
numChars = 0;
lineCount = 0;
}
This is all you need to make your code work.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight= dm.heightPixels;
Replace your following code block with mine. It will work.
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With