anko
anko copied to clipboard
anko layout : generate id by default (especially for constraintLayout)
ConstraintLayout needs its views to have an id to constrain them. I think it's useless to define ids in xml files just for that so I have to write for each view "id = View.generateViewId ()" Why not put this option for all views by default so you do not have to write it every time because anyway, setting an id manually will override this default id.
ConstraintLayout {
icon = imageView{
id = View.generateViewId()
}.lparams {
width = dip(40)
height = dip(40)
}
label = textView {
id = View.generateViewId()
textSize = 18f
typeface = Typeface.DEFAULT_BOLD
}
spendedLabel = textView {
id = View.generateViewId()
textResource = R.string.spended
}
spended = textView {
id = View.generateViewId()
textColorResource = R.color.dark_red
}
budgetLabel = textView {
id = View.generateViewId()
textResource = R.string.budget
}
budget = textView {
id = View.generateViewId()
textColorResource = R.color.primaryColor
}
remainingLabel = textView {
id = View.generateViewId()
textResource = R.string.remaining
}
remaining = textView {
id = View.generateViewId()
textColorResource = R.color.teal
}
progress = horizontalProgressBar {
id = View.generateViewId()
}.lparams { width = matchParent }
applyConstraintSet {
...
+1 The converted to Anko example for Google's ConstraintLayout Codelab only works because of the preexisting IDs defined in the activity_main_done.xml layout file, which is confusing. The Anko code should work even if we delete all of the legacy layout XML files.
MainActivity.kt.txt - working update of MainActivity.kt that does not use data from layout XML files.
+1
+1
+1
+1
+1 need this a lot
+1 must have
+1 It seems like a good idea. Is there any downside to doing this?