0

I am trying to build something like a text reader. It must have a horizontal slide (normal book-ish), not scroll down.

To accomplish that I am dynamically filling a ViewFlipper with many TextViews. Intuitive idea is that I create a new TextView on each previous TextView overflow, and add it to a ViewFlipper.

I might accomplish that with setMaxLines(), but I can`t get the text that goes beyond that max line.

Question:

How can I move text from one TextView to another on overflow?

or

Am I fundamentally wrong with method I decided to use?

EDIT:

Sorry, I cant make my problem clear for some reason. Maybe this will help:

TextView text1, text2;
String s = "Extreamly long string. Much longer than this one";
text1.setText(s);
text2.setText( // Text that is overflowing //);

How do I figure out what part of the String is visible, and what part is overflowing?

Thanks

igorshkov
  • 3
  • 4
  • `filling a ViewFlipper with many TextViews` Well, you actually need only **2**, like the left and right pages of a book (or a diary or a notebook, whatever). Simply fill them appropriately. – Phantômaxx Apr 09 '15 at 11:33
  • That is in fact a helpful note. Thanks. May I also ask you to be a bit more specific about how I move text on overflow from one to another? – igorshkov Apr 09 '15 at 12:06
  • You could cut away the text which is visible in the first TextView from the source string and put the rest in the second TextView. Then, moving forward, replace the first TextView content with that of the second one and in the second one put what remains by cutting the text from the first TextView off the cut string (after cutting the piece in step 1) – Phantômaxx Apr 09 '15 at 12:13
  • Concept also seems right to me. But how do I cut this visible text? – igorshkov Apr 09 '15 at 12:17
  • Use `string.replace()` - Replace the portion of text you want to cut off with `""`. – Phantômaxx Apr 09 '15 at 12:42
  • Yes, but how do I choose this selection? – igorshkov Apr 09 '15 at 12:45
  • hmmmm... what about `txtPageLeft.getText().toString()`? – Phantômaxx Apr 09 '15 at 12:52
  • I guess I did not make myself clear about the question. All the text currently is (say) in txtPageLeft. It overflows. For it not to overflow, it needs to be cut (the visible part). How do I get the visible text? – igorshkov Apr 09 '15 at 12:59
  • Let it overflow. THAT is the visible text. The overflown part is not part of the text anymore. – Phantômaxx Apr 09 '15 at 13:47
  • Please see the edit. You are answering the wrong question. – igorshkov Apr 09 '15 at 14:13
  • Really? what do you get from `txtPageLeft.getText().toString()?` – Phantômaxx Apr 09 '15 at 14:27
  • The whole string. Including the overflow. Don`t you? – igorshkov Apr 09 '15 at 14:58
  • Doesn't it cut away the overflown part? – Phantômaxx Apr 09 '15 at 15:04
  • OK, it seems I'm wrong. Maybe you will find an answer [here](http://stackoverflow.com/questions/8636946/get-current-visible-text-in-textview). – Phantômaxx Apr 09 '15 at 15:06

1 Answers1

0
    TextView tv = new TextView(context);

    ViewTreeObserver vto = tv.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(

            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Layout l = tv.getLayout();

                    int height = tv.getHeight();
                    int scrollY = tv.getScrollY();

                    /**
                     * Key part
                     */

                    int lineCount = l.getLineForVertical(height + scrollY);

                    int start = l.getLineStart(0);
                    int end = l.getLineEnd(lineCount - 1);

                    /**
                     * Cut string
                     */

                    String s = tv.getText().toString().substring(start, end);

                }

            }
    );
igorshkov
  • 3
  • 4