Tuesday, August 9, 2016

Solving the Android findViewById() Problem

One of the first things any Android developer learns is working with layouts. These files, defined in XML, are separate from the Java classes and specify the basis for an app's interactive components. When making the UI interactive, it's necessary to interface between these two files, accessing XML defined views from Java code. To achieve this, the Android SDK uses the concept of IDs.

IDs are XML properties that provide elements with unique identifiers. Each one is stored as a resource to be accessed by other files in the project. In our Java classes, once the view has been inflated, we can access subviews by calling findViewById(). For example, if we wanted a text field to say "Hello, world!", we could do this.


Seems straightforward enough, but if we want to set the text from many different places in the code, typing this would get tedious. It would be a better idea if we could get a reference to the text field once, and then refer to that name later.

Now the text can be set from anywhere, just by referencing the textView property. The view's id is only used once, in onCreate().

With adapters, another common approach to reduce verbosity is the View Holder. Similar to storing global variables, it allows you to reference views by a custom name, instead of an ID. The "holder" is a static class that keeps a reference to all the views, and is useful when working with lists of data, where each view is the same.
With both techniques, referencing views from Java classes is much easier. But after submitting my first project for the Android Developer Nanodegree, the reviewer suggested a different approach. By using the ButterKnife library, findViewById() can be eliminated from the code entirely. Using a simple annotation, view properties can be associated with an ID in one line. The annotations even work with existing view holders, and can be used to bind methods to buttons.
And for the ViewHolder class. Adding additional views only requires another declaration with @BindView.

While you still need to type the view's ID in the Java class, ButterKnife's annotations make the process simple, and require as little code as possible. It's not quite as magical as the @IBOutlet annotations in iOS, which assign IDs behind the scenes, but the result is the same: more readable, more maintainable code.

There are libraries other than ButterKnife, such as Dagger, that allow injection of more than just views. Advanced users may benefit from this additional functionality. But regardless of your skill level, if you're not yet using an injection library, I would highly recommend you give one a try. It will save you a lot of time and make your code much easier to maintain.


11 comments:

  1. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks UC mini app

    ReplyDelete
  2. This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! 9apps games

    ReplyDelete
  3. This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.. ACMarket

    ReplyDelete
  4. It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. Action Link Wireless

    ReplyDelete
  5. Here you Can Download Stock Note 4 Firmware Free With Full Speed

    Galaxy Note 4 Firmware

    ReplyDelete
  6. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Emus4u

    ReplyDelete
  7. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. AirShou iOS 11

    ReplyDelete
  8. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks iFile Download

    ReplyDelete
  9. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Download Cydia iOS 11.3

    ReplyDelete
  10. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. vidmate video

    ReplyDelete
  11. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it, https://www.maddenmobilechampions.com/

    ReplyDelete