Skip to content

Instantly share code, notes, and snippets.

@vontell
Created January 30, 2018 18:03
Show Gist options
  • Save vontell/c85386f68f438b20919d841b9be22df3 to your computer and use it in GitHub Desktop.
Save vontell/c85386f68f438b20919d841b9be22df3 to your computer and use it in GitHub Desktop.
002 ChipView Constructors / Intro
/**
* A simple ChipView class for chips in Android. Provides image, text, and listener
* functionality.
*/
class ChipView: LinearLayout {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
// Load the correct layout for this view
inflate(context, R.layout.view_chip, this)
// Display attributes only after the view has been inflated
val attributes = context.theme.obtainStyledAttributes(
attrs,
R.styleable.ChipView,
defStyleAttr, 0)
try {
// Set the initial text for this label
text = attributes.getString(R.styleable.ChipView_text)
// We set the imageURL last since it takes precedence over a given resource.
// We also still set the resource in case the user wants to use it as a default
imageResource = attributes.getDrawable(R.styleable.ChipView_imageSrc)
imageURL = attributes.getString(R.styleable.ChipView_imageURL)
// We then initially hide the remove icon (a listener may be added later)
displayRemoveIcon()
} finally {
attributes.recycle()
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment