Search code examples
androidkotlinanko

Expected resource of type ID


I am using ANKO to build simple project. When I try to set id for an EditText I am getting following error.

Ensures that resource id's passed to APIs are of the right type; for example, calling Resources.getColor(R.string.name) is wrong.

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    relativeLayout {
        editText{
            id = 1
        }
    }
}
}

click here to view an error image format


Solution

  • You can either use id references (R.id.something) or negative integers from -16777215 to -1 (and ignore the warning).

    Suitable negative integers can also be generated for you by using View.generateViewId() method available since API 17. Source code is available here. You'll have to remember the generated values yourself.

    Generating an R.id is covered in other answers.

    I am trying to do same but getting an error

    The video was published over a year ago and lint didn't warn you about Kotlin code back then.