I have the following extension function to reduce a bit the code and to avoid forgetting the duration when showing a snackbar:
fun Fragment.showSnackbar(text: String, length: Int = Snackbar.LENGTH_SHORT) {
view?.run { Snackbar.make(this, text, length).show() }
}
But the lint is giving me the following error:
Error: Must be one of: BaseTransientBottomBar.LENGTH_INDEFINITE, BaseTransientBottomBar.LENGTH_SHORT, BaseTransientBottomBar.LENGTH_LONG or value must be ≥ 1 (was -1) [WrongConstant]
view?.run { Snackbar.make(this, textResId, length).show() }
~~~~~~
It seems that if you pass a custom length param it should be an int >=0 and it is detecting that the default param is a custom one not a system/class one but it is (LENGTH_SHORT, which is -1).
If the length param is of type BaseTransientBottomBar.Duration
(which is an interface notation to set the possible values of the length param) it compiles well but I don't know how to assign it as a default value.
There is any way to avoid the lint error or to set a default value of type BaseTransientBottomBar.Duration
?
--- EDIT: after apply the @rahat approach:
fun Fragment.showSnackbar(text: String, @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_SHORT) {
view?.run { Snackbar.make(this, text, length).show() }
}
The error:
[PATH]/DesignExt.kt:18: Error: Must be one of: BaseTransientBottomBar.LENGTH_INDEFINITE, BaseTransientBottomBar.LENGTH_SHORT, BaseTransientBottomBar.LENGTH_LONG [WrongConstant]
fun Fragment.showSnackbar(text: String, @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_SHORT) {
~~~~~~~~~~~~~~~~~~~~~
Finally I avoid the error adding the warning 'WrongConstant' to the lint.xml file. If it was the only thing in the file it will be:
android {
lintOptions {
warning "WrongConstant"
}
}