Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.89k stars 1.29k forks source link

anko layout : generate id by default (especially for constraintLayout) #539

Open benjaminledet opened 6 years ago

benjaminledet commented 6 years ago

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 {
            ...
naveedhasan commented 6 years ago

+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.

naveedhasan commented 6 years ago

MainActivity.kt.txt - working update of MainActivity.kt that does not use data from layout XML files.

seaskyways commented 6 years ago

+1

mkubasz commented 6 years ago

+1

ahmafi commented 6 years ago

+1

kiratheone commented 5 years ago

+1

u201701 commented 5 years ago

+1 need this a lot

Rasalexman commented 5 years ago

+1 must have

eitzend commented 5 years ago

+1 It seems like a good idea. Is there any downside to doing this?