Skip to content

ouchadam/attr

Repository files navigation

attr CircleCI Download

Attribute parsing using kapt

compileOnly 'com.github.ouchadam:attr:<latest-version>'
kapt 'com.github.ouchadam:attr-processor:<latest-version>'

Supports

  • @Dimension Float
  • @ColorInt Int
  • @Px Int
  • @IdRes Int
  • Float
  • Boolean
  • Int
  • Drawable

Usage

Declare collections of attributes as data classes

@Attr // mark data class for processing
data class ThemeAttributes(
    @Attr.Id(R.attr.colorPrimary) @ColorInt val colorPrimary: Int, // explicitly specify the attribute id
    @Attr.Id(-1) val missingAttribute: Drawable?, // allow attribute to be unavailable
    @ColorInt val colorPrimaryDark: Int // infer attribute id from parameter name
)

The library adds an extension function to Resources.Theme

val themeAttributes = context.theme.attr<ThemeAttributes>()

An AttributeSet can be provide for usage with a custom view

val customViewAttributes = context.theme.attr<CustomViewAttributes>(attributeSet)