3

I used TextView#getMaxLines() in my application for a few weeks without incident.

Lint is now informing me that it's only available in API 16+ (#setMaxLines() is API 1+...), though (to the best of my knowledge) I haven't modified anything that would cause this sudden flag - my min-sdk has been 8 for a while, and I have files in my source control to prove it.

1) Why could lint be flagging this error randomly? (To be clear, I mean to say that it should have caught it initially - I'm not implying this is something that it shouldn't have flagged at all).

2) Is there any way to retrieve the maxLines for a TextView on pre-api 16 devices? I checked the source but couldn't devise a way to retrieve this value using the exposed methods on a 2.2 device.

3 Answers 3

26

A simpler solution was added to the support lib v4 inTextViewCompat

int maxLines = TextViewCompat.getMaxLines(yourtextView);

Check out this answer for some more informations.

2
  • 1
    in my opinion this answer is best and should be accepted
    – m.zander
    Commented Dec 11, 2015 at 11:41
  • Works fine for me ! Commented Jun 30, 2016 at 12:55
4

You can use Reflection:

Field mMaximumField = null;
Field mMaxModeField = null;
try {
    mMaximumField = text.getClass().getDeclaredField("mMaximum");
    mMaxModeField = text.getClass().getDeclaredField("mMaxMode");
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

if (mMaximumField != null && mMaxModeField != null) {
    mMaximumField.setAccessible(true);
    mMaxModeField.setAccessible(true);

    try {
        final int mMaximum = mMaximumField.getInt(text); // Maximum value
        final int mMaxMode = mMaxModeField.getInt(text); // Maximum mode value

        if (mMaxMode == 1) { // LINES is 1
            text.setText(Integer.toString(mMaximum));
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

OR:

Maybe, the best way is keep maxLine value at values and set it value in xml, and get as int resource in code.

8
  • 1
    I agree with your "But, maybe" part, and this is how I was planning to work around the issue. I don't think there's any point using reflection because I wouldn't have been able to handle pre-API 16 devices without manually keeping track of the values in addition to using getMaxLines() for API16+ (in which case it would make the latter redundant).
    – ataulm
    Commented Apr 27, 2013 at 20:11
  • ah wait, I think I entirely misunderstood. (If you remove the bulletpoint markup, it fixes your code formatting for SO.) If mMaximum is private, would it still get the correct value at the time of the call?
    – ataulm
    Commented Apr 27, 2013 at 20:15
  • @ataulm for getting private field value you just need to set Field.setAccessible(true). Sorry for the poor formatting:(
    – nfirex
    Commented Apr 27, 2013 at 20:46
  • Hi, I am trying to use reflection technique but getDeclaredField() returns null and causes exception right away even though the class includes those variables. Any ideas?
    – ahmad
    Commented Dec 2, 2014 at 21:40
  • @ahmad, do you use reflection in this class and on this fields? or it is not working in another class?
    – nfirex
    Commented Dec 3, 2014 at 8:35
1

The code for that method simply doesn't exist on 2.2, so you can't use it directly of course.

On the other hand, I've run a diff on the two files and it seems as though the new 4.2.2 TextView isn't using any new APIs internally (this is based solely on its imports). You may be able to add it as a class in your project and use it instead of the inbuilt TextView across all version of Android.

2
  • I was actually planning on subclassing TextView anyway so I could specify fonts in XML, so this sounds like a good idea. I suppose there would be minimal overhead with adding this as a class in my project? (And even no difference in performance at all if I subclassed the 4.2.2 TextView (after including it) instead of the 2.2 one?) +1 for the preliminary check for compatibility, I'll include it and test it on an API8 emulator.
    – ataulm
    Commented Apr 27, 2013 at 19:14
  • I was unable to include the new version directly; a lot of the imports couldn't be resolved. I tried also with copying the 2.2 TextView class, and the same imports couldn't be resolved (e.g. import android.content.res.CompatibilityInfo;)
    – ataulm
    Commented Apr 27, 2013 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.