1

I have a large String from the database. I showed it in the TextView with scrolling.

This is a totally boring method to show large text though; I want to show data in pages.

There are multiple questions similar to this one, but I am not able to find any solutions from them; they are incomplete and off track.

Challenges

  1. How to count number of lines that fit to screen?
  2. How to split string in sub parts so that each substring fits to a page?
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Tarun Sharma
  • 824
  • 1
  • 11
  • 24
  • Kind of like the large text scrolling in this question? – Code-Apprentice Sep 14 '14 at 08:51
  • I guess now you liked it. – Tarun Sharma Sep 14 '14 at 08:54
  • The data itself should probably help you out with this. Consider establishing a reasonable amount of data to be presented on a "page". A page in a book is a page regardless of the reading method, IOW, don't establish pages dynamically. Establish a page, display the same page on every device allowing for scrolling and paging. – ChiefTwoPencils Sep 14 '14 at 09:15
  • You can't solve this as (I think) you want to. A "no-scroll page size" is limited by the available display area, depends on the font size, number of lines, and baseline distance. Long lines make for bad reading; if the font is too small, I won't read it at all. -- If you want pages, provide buttons for "up" and "down" (plus the scroll bar), size settings, width and height change, adjusting the text by wrapping (no horizontal scrolling!!) -- This will give "best service". – laune Sep 14 '14 at 10:50
  • TextView is auto-Scroll able. I want to show large String or data in Screen without scrolling. if there is large data data should show fit to screen and remaining subString should show on next pages on user action I don't know what you people are discussing ?. – Tarun Sharma Sep 14 '14 at 12:58
  • 1
    I don't think there is an exact method to divide string into multiple pages based on its rendered size. You can, however, calculate how text height will be (http://stackoverflow.com/a/14278893/1019366). You can do binary-search or whatever to divide string so that it can fit into pages. – Kazuki Oct 05 '14 at 22:02

1 Answers1

0

I think this should solve the problem, 1. it will split the large string 2. display on view as you want

So if u dont want to use Scroll View then well i tried something and i hope it helps

Following i done 1. inflate required views into parent view

2. set large text into Textview 3. Check if other text views are within the (visible) screen or not 4. update appropriately

Main Activity class

public class MainActivity extends Activity {

private TextView text1, text2;
private Context context;
private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getLayoutInflater().getContext();
    layout = (LinearLayout) findViewById(R.id.parentLayout);
    setTextViews();
    // checkViewAndUpdate();
    layout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    checkViewAndUpdate();
                }
            });

}

private void checkViewAndUpdate() {

    Rect rect = new Rect();
    layout.getHitRect(rect);

    if (text1.getLocalVisibleRect(rect)) {
        Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
    } else {
        // update if not visible decreasing the view text size
        Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
    }
    if (text2.getLocalVisibleRect(rect)) {
        Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
    } else {
        // update if not visible decreasing the view text size
        Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
    }

}

private void setTextViews() {

    text1 = new TextView(context);
    text2 = new TextView(context);

    text1.setText("your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreateyour link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate ");
    text2.setText("text2");

    text1.setTextSize(30);
    text2.setTextSize(30);

    // layout.removeAllViews();

    layout.addView(text1);
    layout.addView(text2);
}

}

wizyashas
  • 95
  • 1
  • 12