I am developing an e-book reader application for Android 3.0 tablets. For starters, I have a large piece of String data. I want to split / split this line into pages based on the device screen size [I plan to use a text switch or flipper]. Although I tried using the getWindowManager () method, I could not get the preferred results.
The next thread mentions that Text Switcher automatically breaks text according to screen size. But I do not think so. Managing text in an Android application, like in an e-book
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; }
pkamalaruban
source share