Required custom attributes
TypedArray also has a few methods to cover both attribute getting and exception throwing states. Just use them if you want your attribute to be required (not clearly)
Using Android KTX, there are extension functions that provide getFloatOrThrow()
and other similar methods.
Check them out here
AFAIK, currently attr
s don't have any attributes to mark them mandatory. ("layout_width"
and the like are very common mistakes, checking for those is wired into the SDK)
You can, however, throw an RuntimeException
from your custom View
if an attribute is missing (which you can determine by calling TypedArray.hasValue()
) -- TypedArray
does that in getLayoutDimension()
method.
This is exactly the behavior you get if you compile an app that's missing a "layout_width"
somewhere using command line tools (i.e. no Eclipse or Idea to detect the missing atrribute and prevent you from compiling): you run the app and it crashes immediately with a RuntimeException
saying you're missing the attribute.