Is it possible to use TextView # getMaxLines () on pre api-16 devices?

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

Lint now tells me that it is only available in API 16+ ( #setMaxLines() is an API 1 + ...), although (as far as I know) I have not modified anything that could cause this sudden flag - my min- sdk was 8 for a while, and I have files in my source control to prove it.

1) Why can lint flag this error randomly? (To be clear, I want to say that he had to catch him initially - I do not mean that this is what he should not have noted at all).

2) Is there a way to extract maxLines for TextView on pre-api 16 devices? I checked the source , but could not create a way to get this value using public methods on the 2.2 device .

+3
source share
3 answers

A simpler solution has been added to support lib v4 in TextViewCompat

 int maxLines = TextViewCompat.getMaxLines(yourtextView); 

Check out this one for more information.

+25
source

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

Perhaps the best way is to save the maxLine value at the values ​​and set its value in xml and get as an int resource in the code.

+4
source

The code for this method simply does not exist on 2.2, so you cannot use it directly, of course.

On the other hand, I ran diff for two files, and it seems that the new 4.2.2 TextView does not use any new APIs inside (this is based solely on its import). You can add it as a class to your project and use it instead of the built-in TextView for the entire version of Android.

+1
source

Source: https://habr.com/ru/post/1237834/


All Articles