'How do I (continue to, post-Kotlin Android Extensions) access widgets as array elements (which are doubly-included)?
I am "modernizing" some 15-month-old code to take advantage of Kotlin Extension View Binding (migrating away from the deprecated Kotlin Android Extensions compiler plugin).
The issue I'm having is related to the practice of using vars of
type Array<ConstraintLayout> throughout my code. This is
exemplified by charKeys throughout this posting.
I'm using nested includes within the XML.
I am struggling to figure the correct new syntax or approach. I cannot get this code to compile yet.
NOTE: All Kotlin & XML has been reduced to relevant sections only.
First, the "old" way - which is working perfectly.
PuzzleFragment.kt
import kotlinx.android.synthetic.main.fragment_puzzle.*
import kotlinx.android.synthetic.main.item_keyboard.*
import kotlinx.android.synthetic.main.item_keyboard.view.*
import kotlinx.android.synthetic.main.item_kkey.view.*
:
class PuzzleFragment : Fragment() {
lateinit var charKeys: Array<ConstraintLayout>
charKeys = arrayOf(
kbd_char_0,
kbd_char_1
:
)
fragment_puzzle.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/puzzle_fragment"
>
<include layout="@layout/item_keyboard" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_keyboard.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/keyboard"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/kbd_char_0"
>
<include layout="@layout/item_kkey" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/kbd_char_1"
>
<include layout="@layout/item_kkey" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
item_kkey.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/kkey"
/>
</merge>
Again, all of that is (was) working (using Kotlin Android Extensions). And this code allows (allowed) me to do things like:
PuzzleFragment.kt
for (x in 0 until someNumber) {
val shape = charKeys[x].background as GradientDrawable
shape.setStroke(...)
charKeys[x].kkey.setTextColor(...)
for (key in charKeys)
key.isEnabled = false
for ((kx, key) in charKeys.withIndex())
key.elevation = ... //using kx
for (cx in 0 until maxGuessLength)
makeKeyRed(charKeys[cx], true)
private fun makeKeyRed(key: ConstraintLayout, doRed: Boolean) {
when {
doRed -> key.kkey.setTextColor(...)
key.kkey.text != "#" -> key.kkey.setTextColor(...)
else -> key.kkey.setTextColor(...)
}
}
So - that's the old way. Everything's cool. Now I'm converting this code. I have:
PuzzleFragment.kt
import com.zazzem.thats.databinding.FragmentPuzzleBinding
class PuzzleFragment : Fragment(R.layout.fragment_puzzle) {
private var _binding: FragmentPuzzleBinding? = null
private val binding get() = _binding!!
The following code seems ok (no "highlighted" errors in the IDE):
lateinit var charKeys: Array<ConstraintLayout>
charKeys = arrayOf(
binding.item_keyboard.kbd_char_0,
binding.item_keyboard.kbd_char_1,
:
)
for (x in 0 until someNumber) {
val shape = charKeys[x].background as GradientDrawable
shape.setStroke(...)
But it "falls apart" with the next line of code:
charKeys[x].kkey.setTextColor(...)
It doesn't like "kkey" here ("Unresolved reference"). And, in fact, Basic code completion (Ctrl + Space) shows none of the widgets which are part of item_kkey.
I don't know if I'm simply missing something obvious? Or if this whole "array of 'vaguely-typed' ConstraintLayouts" approach isn't valid (any longer)? Or if it's something in between?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
