2

Previously, I asked a question titled: "How to paginate text in Android" on stackoverflow, which lead to a great answer that helped me so much. Now I need to port the code on the older Android API, and in case, Android API version 8, so I can run my application on mostly all Android devices, starting from Android 2.2.

The problem I come across is that some methods used in the answer are only available in Android API 16+.

        mPagination = new Pagination(mText,
                mTextView.getWidth(),
                mTextView.getHeight(),
                mTextView.getPaint(),
                mTextView.getLineSpacingMultiplier(),
                mTextView.getLineSpacingExtra(),
                mTextView.getIncludeFontPadding());

So, I have to implement at least these methods:

                TextView.getLineSpacingMultiplier()
                TextView.getLineSpacingExtra()
                TextView.getIncludeFontPadding()

The second one seems to be already described here as:

mTextView.getPaint().getFontSpacing()
* mTextView.getLineSpacingMultiplier() + mTextView.getLineSpacingExtra()

But I don't know if the other two are relevant in 2.2, and if they are, how to implement them in Android API version 8. There is some hint here, using the source code of Android TextView which I could not fully understand.

Community
  • 1
  • 1
Ho1
  • 1,239
  • 1
  • 11
  • 29
  • see http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/widget/TextView.java#3587 and http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/widget/TextView.java#3601 and http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/widget/TextView.java#6542 – pskink Sep 13 '15 at 06:12
  • @pskink Tracing the sources of change for these private fields was not easy, so I just replaced them as their default value: `1.0f`, `0.0f`, and `true` to make it work. But I don't know the probable side effects. – Ho1 Sep 13 '15 at 06:23
  • @pskink These methods return private fields. Copying the default value of these fileds was easy, but tracing the changes of these fields through the code, and copying the exact same behaviour of changing these fields according to the configuration was not easy, at least to me. For example, in case `attr==com.android.internal.R.styleable.TextView_lineSpacingMultiplier`, then `mSpacingMult = a.getFloat(attr, mSpacingMult)` which depends on `attr` and `a` which `a=context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes)` and this dependency goes on. – Ho1 Sep 13 '15 at 06:37
  • @Onik Sorry, and thanks for your attention to my question. :-/ I figured out how to (partially) fix the problem by using the default value, and ignoring the rest of the code! I want to ask another question regarding your solution, about RTL issues that I had there. – Ho1 Oct 01 '15 at 14:52
  • @Ho1 Ok then. If you found a solution you might want to post an answer to your own question or, in case it's not a solution, just a workaround to the problem, edit the question posting the new result. Regarding the RTL issue, you also might ask a new question in order to get a better solution...than the note [here](http://developer.android.com/reference/android/text/Layout.html#getLineDirections(int)) – Onik Oct 01 '15 at 15:03

1 Answers1

2

If we look at the TextView's sources we can see the following.

In Android 5.0:

private float mSpacingMult = 1.0f;
private float mSpacingAdd = 0.0f;
...
private boolean mIncludePad = true;

In Android 2.0:

private float mSpacingMult = 1;
private float mSpacingAdd = 0;
...
private boolean mIncludePad = true;

So the default values are equal.

With that said, you can use the Pagination's constructor with the default values which would work for API 8+:

new Pagination(mText,
            mTextView.getWidth(),
            mTextView.getHeight(),
            mTextView.getPaint(),
            1.0f, 0.0f, true);

In case you let a user change/set (via a settings menu) these parameters, again, no need to use the getters. You simply set a new value(s) of parameter(s) utilizing one of the TextView's setters and pass the same value(s) to the Pagination's constructor. For instance,

new Pagination(mText,
            mTextView.getWidth(),
            mTextView.getHeight(),
            mTextView.getPaint(),
            newValueOfSpacingMultiplier,
            newValueOfSpacingExtra,
            newValueOfIncludeFontPadding);

EDIT

What I'm trying to say is that there is no need "to implement them in Android API version 8". Once set the parameters never change within TextView's lifecycle. You can check it with a simple test. Just look at their values before setting any text (for example, in onCreate() of the sample code) and after (in onGlobalLayout() right after mPagination initialization or in the update() method).

The idea of passing all the parameters of TextView to the Pagination's constructor is to use identically configured instances of the Layout class under the hood of both TextView and Pagination. So instead of passing the parameters to Pagination's constructor previously retrieved from TextView with getters, you could achieve the identically configured Layout by passing whatever values you'd like to Pagination and setting the same values to TextView with proper setters that are already met in API version 8.

Community
  • 1
  • 1
Onik
  • 19,396
  • 14
  • 68
  • 91
  • @Ho1 But, from _"...tracing the changes of these fields through the code, and copying the exact same behaviour of changing these fields..."_ I seem like by your opinion these parameters change during text processing within `TextView`. Am I right? Cause I haven't noticed anything like that in the sources so as while testing it. Namely, if we look at these 3 parameters right after `TextView` has been initialized and then later in `onGlobalLayout()` (the place where the view has already been drawn with text, inlcuding a spanned one), the values of the parameters are the same. – Onik Oct 02 '15 at 21:53
  • @Ho1 So, to sum up, by me it's no need to worry about _"porting the code on the older Android API"_, just use them as per the answer. – Onik Oct 02 '15 at 21:59
  • @Ho1 Keep editing the answer in a hope to clarify my thoughts about using the `Pagination` class. Really interested in hearing your opinion or, probably, doubts? :) – Onik Oct 03 '15 at 17:59