attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 4) doesn't support to get max length int value from an integer id.
Insight 👀
The default way to support getting int value from int id value is using TypeArray.getInt(com.android.internal.R.styleable.TextView_maxLength, -1), but custom PinEntryEditText class can't access to com.android.internal.R.styleable.TextView_maxLength.
There is another approach to get the current value of max length from android:maxLength property. android:maxLength was initialized via TextView into InputFilter.LengthFilter, so we could get this value out from current filter list. Note that, InputFilter.LengthFilter.getMax() is only supported from API 21, so we need to use reflection to get mMax value out in lower APIs. mMax field was introduced from the first version of InputFilterhttps://android.googlesource.com/platform/frameworks/base/+/54b6cfa9a9e5b861a9930af873580d6dc20f773c/core/java/android/text/InputFilter.java
After rechecked, it seems getting value out from mMax by reflection on API 19 is not a pretty way, back to getting value from resid or raw int value https://stackoverflow.com/a/60593495.
What happened 🤔
attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 4)
doesn't support to get max length int value from an integer id.Insight 👀
The default way to support getting int value from int id value is using
TypeArray.getInt(com.android.internal.R.styleable.TextView_maxLength, -1)
, but customPinEntryEditText
class can't access tocom.android.internal.R.styleable.TextView_maxLength
.There is another approach to get the current value of max length from
android:maxLength
property.android:maxLength
was initialized viaTextView
intoInputFilter.LengthFilter
, so we could get this value out from current filter list. Note that,InputFilter.LengthFilter.getMax()
is only supported from API 21, so we need to use reflection to getmMax
value out in lower APIs.mMax
field was introduced from the first version ofInputFilter
https://android.googlesource.com/platform/frameworks/base/+/54b6cfa9a9e5b861a9930af873580d6dc20f773c/core/java/android/text/InputFilter.javaAfter rechecked, it seems getting value out from
mMax
by reflection on API 19 is not a pretty way, back to getting value from resid or raw int value https://stackoverflow.com/a/60593495.PoW (a.k.a Proof Of Work)💪
API 21
API 19