After converting some Java code to Kotlin (to create a ReplacementSpan), an annotation-related error is returned.
from
in the @IntRange(from = 0)
constructor returns an error:
'IntRange' is not an annotation class | Cannot find a parameter with this name: from
import android.graphics.Canvas
import android.graphics.Paint
import sun.swing.SwingUtilities2.drawRect
import android.text.style.ReplacementSpan
class HrSpan : ReplacementSpan() {
override fun getSize(
paint: Paint, text: CharSequence, @IntRange(from = 0) start: Int,
@IntRange(from = 0) end: Int, fm: Paint.FontMetricsInt?
): Int {
return 0
}
override fun draw(
canvas: Canvas, text: CharSequence, @IntRange(from = 0) start: Int,
@IntRange(from = 0) end: Int, x: Float, top: Int, y: Int, bottom: Int,
paint: Paint
) {
canvas.drawRect(x, top.toFloat(), y.toFloat(), (top + 8).toFloat(), paint)
}
}
Kotlin confuses its own IntRange
class with the IntRange
annotation of the Android SDK.
Make an import like that giving it another name:
For Android Support library:
import android.support.annotation.IntRange as AndroidIntRange
For AndroidX:
import androidx.annotation.IntRange as AndroidIntRange
and use it like this:
fun foo(@AndroidIntRange(from = 0, to = 255) bar: Int) {
}