1

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)
    }
}

1 Answer 1

5

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) {
}
Sign up to request clarification or add additional context in comments.

1 Comment

for AndroidX you must import androidx.annotation.IntRange as AndroidIntRange developer.android.com/reference/kotlin/androidx/annotation/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.