You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
I'm having trouble referring to views outside the current scope.
My use case is to call an EditText from a button that lives in a different layout/ViewGroup.
Please look at the below examples to see what I am trying to accomplish. It's possible that there's a way that I'm not seeing.
Example A - Button and EditText as siblings
Works! :)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
val name = editText() {
id = nameId
}
button("Register") {
onClick {
toast("Hello, ${name.text}!")
}
}
}
}
Example B - Button and EditText in separate ViewGroups
Won't compile - name is used outside scope
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
relativeLayout {
verticalLayout {
val name = editText() {
id = nameId
}
}
button("Register") {
onClick {
toast("Hello, ${name.text}!")
}
}
}
}
Example C - Passing instantiated EditText into DSL
Runs, but name doesn't create an actual instance of editText inside relativeLayout. So there's no editText in the UI.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val name = editText() {
relativeLayout {
verticalLayout {
val name = editText() {
id = nameId
}
}
button("Register") {
onClick {
toast("Hello, ${name.text}!")
}
}
}
}
Example D - findViewById
Crash at runtime - findViewById returns a View, not an EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val name = editText() {
val nameId = 1
relativeLayout {
verticalLayout {
val name = editText() {
id = nameId
}
}
button("Register") {
onClick {
// fails with: android.view.View! cannot be cast to T
val name = find<EditText>(nameId)
// fails with: android.view.View! cannot be cast to android.widget.EditText
val name: EditText = findViewById(nameId) as EditText
toast("Hello, ${name.text}!")
}
}
}
}
Many thanks for your time and attention. Let me know how I can help get this resolved.
The text was updated successfully, but these errors were encountered:
Anko team,
Thanks for the great library. :)
I'm having trouble referring to views outside the current scope.
My use case is to call an EditText from a button that lives in a different layout/ViewGroup.
Please look at the below examples to see what I am trying to accomplish. It's possible that there's a way that I'm not seeing.
Example A - Button and EditText as siblings
Works! :)
Example B - Button and EditText in separate ViewGroups
Won't compile - name is used outside scope
Example C - Passing instantiated EditText into DSL
Runs, but name doesn't create an actual instance of editText inside relativeLayout. So there's no editText in the UI.
Example D - findViewById
Crash at runtime - findViewById returns a View, not an EditText
Many thanks for your time and attention. Let me know how I can help get this resolved.
The text was updated successfully, but these errors were encountered: