Kotlin / anko

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

How to get reference of custom ids in values/ids.xml #418

Open Rahulkr2 opened 7 years ago

Rahulkr2 commented 7 years ago

I'm using anko in recyclerView adapter for creating viewholder's view. I have done this successfully but don't know how to refer it using kotlin synthetic by view id (I want to get it without findViewById)

value/ids.xml `

<item name="txv2" type="id"/>

`

My Anko getView Codes: ` private fun getView(context: Context): View{ return with(context){ linearLayout { lparams(width = matchParent, height = wrapContent) padding = dip(10) orientation = android.widget.LinearLayout.HORIZONTAL

                //Task Number
                textView {
                    id = R.id.txv1
                    text = "TextView 22"
                    textSize = 16f
                    typeface = Typeface.MONOSPACE
                    padding =dip(5)
                }.lparams(){
                    weight = 1f
                }

                //Task Name
                textView {
                    id = R.id.txv2
                    text= "TextView 33"
                    textSize = 16f
                    typeface = android.graphics.Typeface.DEFAULT_BOLD
                    padding =dip(5)
                }
            }
        }
    }

`

I'm assigning custom ids from ids.xml but how to get it without findViewById

Thanks`

kirillkh commented 7 years ago

I ran into the same problem with Anko. Currently using static constants for ids as a workaround. E.g.:

object Id {
  @IdRes val toolbar = View.generateViewId()
  @IdRes val container = View.generateViewId()
  // ...
}

But I don't like adding a custom place to store ids, when there is a standardized location: ids.xml.