Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in Android TextView

I have some text in a file [~100 KB] that needs to be displayed to the user in a TextView. I want to split the text into pages.

This is the idea I have in mind to implement pagination:

  • Determine screen width and screen height, say 320 x 480.
  • Calculate 75% of height, [360 px] to accommodate buttons etc.
  • Determine font size
  • Calculate number of characters [N] that can be displayed.
  • Read from file and display only N number of characters.

This seems like it would work, but it seems crude and error-prone. Does anyone have better ideas?

like image 852
WeNeigh Avatar asked Jun 03 '11 08:06

WeNeigh


1 Answers

You should create your own extension of TextView and hook up into the onMeasure function which should give you the width and height (prob want to give the textview layout_weight=1)

Then you can use a paint to get the size that text will take up, not I've not tested this and you'll probably need to do something to account for splitting of newlines. But it is a good start...

Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(12);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_check_h =  bounds.height(); // Will give you height textview will occupy
text_check_w =  bounds.width();  // Will give you width textview will occupy
like image 133
stealthcopter Avatar answered Nov 04 '22 15:11

stealthcopter