1

When designing view controllers in XCode you can easily bind a variable in the class with an element in the storyboard editor they are refered to as @IBOutlets and get linked to the interface when the view controller is loaded.

In android so far I find myself having to call findViewById in the onCreate method of an activity and then manually bind all instance variable to their interface xml counterpart.

Is it possible to define in the layout file the corresponding variable for the interface element so that they get linked together when instantiated without having to call findViewById later in the onCreate?

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • I think Android does not provide such functionality out of the box. There may be some libraries, if you are really interested. For a Button though, you can use onClick attribute in xml to link the button to a method in java. – drWisdom Feb 16 '16 at 16:39
  • Yes the onclick works fine indeed. However I must get references to edittext and textviews – Nicolas Manzini Feb 16 '16 at 16:40
  • 2
    Take a look at `ButterKnife` library, It's the closet thing to IBOutlets & widely used: http://jakewharton.github.io/butterknife/ – Sharjeel Feb 16 '16 at 16:44
  • 1
    It is unclear exactly the scope you are seeking. http://developer.android.com/tools/data-binding/guide.html or http://jakewharton.github.io/butterknife/ might be candidates. – CommonsWare Feb 16 '16 at 16:44
  • Some addition to prev comments: in 2019 you have some less old-school options like Kotlin synthetic imports or DataBinding: https://stackoverflow.com/a/58155708/6405022 – kkaun Oct 13 '19 at 10:45

0 Answers0