2

I am trying to display a long string into multiple pages. My Activity.xml has a textview which occupies 90% of the height of screen(this is ow I have set the relativelayout). I need some help in finding out the right methods/classes I can use to acheive my objective

Below is my code. The content in bold (comment starting with ** down in the code) is where I need help.

public class StoryActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    String contentString;
    String pageString;
    final int screenWidth;
    final int screenHeight;
   final int pageWidth;
   final int pageHeight;
   final int totalPages;
   int pageNumber;


    //load the content into the string contentString    
    InputStream is = getAssets().open("Story.txt");
        int size = is.available();
       byte[] buffer = new byte[size];
       is.read(buffer);
       is.close();
     contentString = new String(buffer);

           // get screen dimensions       
       DisplayMetrics dm = new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(dm);
       screenWidth = dm.widthPixels;
       screenHeight= dm.heightPixels;

       // create textview
       TextView tv = (TextView)findViewById(R.id.textViewStory);

             // calculate pageheight since textview size is 90% of the screen 
            pageHeight = (int) (0.9* screenHeight);
            pageWidth = screenWidth;

            **// 1)need to find number of lines which will fit pageHeight
            // 2)then I need to find total number of lines in my String contentString
            // 3)dividing 2) by 1) can give me number of pages required to display 
            // contentString. 
            // 4)I can then concatenate contentString at number of lines as found in 
            // 1) and thus I know what text (pageString) to display in page1**

            tv.setText(pageString); 

            **// steps 4) can be repeated to display next page when user click 
            // “nextpage” button**         

    }
user1938357
  • 1,466
  • 3
  • 20
  • 33

1 Answers1

2

I will not provide you full solution but I've found answers that can help you.

  1. First helps to find how many characters can feet in TextView width.
  2. Second helps to get how many lines TextView exactly shows.

But there is problem with line wrapping. You need to apply some algorithm to find out how text is wrapped by android framework or do this for it. Here is good answer for text wrapping

Community
  • 1
  • 1
Nazarii Bardiuk
  • 4,272
  • 1
  • 19
  • 22
  • thats very helpful. I went through both the links you have shared. I am sure I can figure my wya out using them. One small question however, in case you can clarify. How do I figure out the default font android uses for a textview, or the pixel width/height for any font. Becuase then only I can find out the character width/height as well. – user1938357 Jan 07 '13 at 07:18
  • 14sp http://stackoverflow.com/questions/4285225/what-are-the-default-font-caracteristics-in-android/4285644#4285644 – Nazarii Bardiuk Jan 07 '13 at 08:40