{"id":899,"date":"2013-01-01T21:16:38","date_gmt":"2013-01-02T05:16:38","guid":{"rendered":"http:\/\/www.andreanolanusse.com\/en\/?p=899"},"modified":"2013-01-02T12:27:02","modified_gmt":"2013-01-02T20:27:02","slug":"android-findviewbyid-vs-injectview","status":"publish","type":"post","link":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/","title":{"rendered":"Android findViewById vs @InjectView"},"content":{"rendered":"<p>When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById inconvenient, as your start using you tend to repeat the same code several times, it add more lines of code, make difficulty to read, maintain, etc. However there is a solution for that called @InjectView and it&#8217;s part of  <a href=\"http:\/\/code.google.com\/p\/roboguice\/\" target=\"_blank\">RoboGuice<\/a> and eliminate this issue and make your code cleaner, smaller and replace the use of\u00a0findViewById, let&#8217;s see how it works.<\/p>\n<p>Let&#8217;s assume you defined the following edit text:<\/p>\n<pre class=\"brush: xml\">\r\n<EditText android:text=\"First Name\" \r\n    android:id=\"@+id\/firstnameEditText\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\/>\r\n<\/pre>\n<p>We would use the following code to access the edit text using findViewById.<\/p>\n<pre class=\"brush: java\">\r\nclass Main extends Activity { \r\n\r\n    public void onCreate(Bundle savedInstanceState) { \r\n        super.onCreate(savedInstanceState); \r\n        setContentView(R.layout.main);\r\n        EditText firstname = null;\r\n        firstname = (EditText) findViewById(R.id.fistnameEditText);\r\n        firstname.setText( \"My first name is Barack\"); \r\n    } \r\n}\r\n<\/pre>\n<p>In order to use @InjectView we have to extend from RoboActivity class (download the RoboGuice from <a href=\"http:\/\/code.google.com\/p\/roboguice\/source\/checkout\" target=\"_blank\">here<\/a>), we just need to declare our class fields using @InjectView, set each field to the correspondent view object declared on our R.Java, the code will look like this.<\/p>\n<pre class=\"brush: java\">\r\nclass Main extends RoboActivity { \r\n     @InjectView (R.id.firstnameEditText)      EditText firstname; \r\n\r\n    public void onCreate(Bundle savedInstanceState) { \r\n        super.onCreate(savedInstanceState); \r\n        setContentView(R.layout.main);\r\n        firstname.setText( \"My first name is Barack\" ); \r\n    } \r\n}\r\n<\/pre>\n<p>In layouts where we have multiple objects, it will became more visible the benefits of replacing findViewById per @InjectView, check out the following code where I add some extra controls to my layout.<\/p>\n<pre class=\"brush: java\">\r\nclass Main extends RoboActivity { \r\n     @InjectView (R.id.firstnameEditText)  EditText firstname; \r\n     @InjectView (R.id.lastnameEditText)   EditText lastname; \r\n     @InjectView (R.id.photoImage)         ImageView photo; \r\n\r\n    public void onCreate(Bundle savedInstanceState) { \r\n        super.onCreate(savedInstanceState); \r\n        setContentView(R.layout.main);\r\n        firstname.setText( \"My first name is Barack\" ); \r\n        lastname.setText( \"My last name is Obama\" ); \r\n        photo.setImageBitmap( null ); \r\n    } \r\n}\r\n<\/pre>\n<p>Hope this small tip can help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById inconvenient, as your start using you tend to repeat the same code several times, it add more lines of code, make difficulty to read, maintain, etc. However there is a solution for that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_s2mail":"yes","footnotes":""},"categories":[92],"tags":[45],"class_list":["post-899","post","type-post","status-publish","format-standard","hentry","category-android","tag-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development<\/title>\n<meta name=\"description\" content=\"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development\" \/>\n<meta property=\"og:description\" content=\"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\" \/>\n<meta property=\"og:site_name\" content=\"Andreano Lanusse | Technology and Software Development\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-02T05:16:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-02T20:27:02+00:00\" \/>\n<meta name=\"author\" content=\"Andreano Lanusse\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andreano Lanusse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\"},\"author\":{\"name\":\"Andreano Lanusse\",\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b\"},\"headline\":\"Android findViewById vs @InjectView\",\"datePublished\":\"2013-01-02T05:16:38+00:00\",\"dateModified\":\"2013-01-02T20:27:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\"},\"wordCount\":197,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b\"},\"keywords\":[\"Android\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\",\"url\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\",\"name\":\"Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development\",\"isPartOf\":{\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#website\"},\"datePublished\":\"2013-01-02T05:16:38+00:00\",\"dateModified\":\"2013-01-02T20:27:02+00:00\",\"description\":\"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById\",\"breadcrumb\":{\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.andreanolanusse.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android findViewById vs @InjectView\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#website\",\"url\":\"https:\/\/www.andreanolanusse.com\/en\/\",\"name\":\"Andreano Lanusse | Technology and Software Development\",\"description\":\"Where Andreano Lanusse talk about technology, software development, programming techniques, databases, games and more through articles, tutorials and videos\",\"publisher\":{\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.andreanolanusse.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b\",\"name\":\"Andreano Lanusse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/49ab23ef70c249c0cb3469f14ef07edc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/49ab23ef70c249c0cb3469f14ef07edc?s=96&d=mm&r=g\",\"caption\":\"Andreano Lanusse\"},\"logo\":{\"@id\":\"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/image\/\"},\"description\":\"Andreano Lanusse is an expert and enthusiastic on software development industry, at Embarcadero he is focused on helping to make sure the products being developed meet the expectations of Embarcadero's customers, as well as defining market strategies for Latin America. Today as Latin Lead Evangelist he spends great deal of time in developer conferences, tradeshows, user group, and visiting customers throughout Latin America. Before Embarcadero, he worked 13 years for Borland, Andreano has worked as Support Coordinator, Engineer, Product Manager, including Product Line Sales Manager, where was responsible to manage the relationship with Brazil developer community, also has worked as Principal Consultant for Borland Consulting Services on the development and management of critical applications. He previously served as Chief Architect for USS Solu\u00e7\u00f5es Gerenciadas (now USS Tempo). Andreano holds a bachelor's degree in Business Administration Marketing Emphasis from Sumare Institute, MBA in Project Management from FGV, certification in Microsoft products, all Borland ALM products, and all CodeGear product line.\",\"sameAs\":[\"http:\/\/www.andreanolanusse.com\",\"https:\/\/x.com\/andreanolanusse\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development","description":"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/","og_locale":"en_US","og_type":"article","og_title":"Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development","og_description":"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById","og_url":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/","og_site_name":"Andreano Lanusse | Technology and Software Development","article_published_time":"2013-01-02T05:16:38+00:00","article_modified_time":"2013-01-02T20:27:02+00:00","author":"Andreano Lanusse","twitter_misc":{"Written by":"Andreano Lanusse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#article","isPartOf":{"@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/"},"author":{"name":"Andreano Lanusse","@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b"},"headline":"Android findViewById vs @InjectView","datePublished":"2013-01-02T05:16:38+00:00","dateModified":"2013-01-02T20:27:02+00:00","mainEntityOfPage":{"@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/"},"wordCount":197,"commentCount":4,"publisher":{"@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b"},"keywords":["Android"],"articleSection":["Android"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/","url":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/","name":"Android findViewById vs @InjectView | Andreano Lanusse | Technology and Software Development","isPartOf":{"@id":"https:\/\/www.andreanolanusse.com\/en\/#website"},"datePublished":"2013-01-02T05:16:38+00:00","dateModified":"2013-01-02T20:27:02+00:00","description":"When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById","breadcrumb":{"@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.andreanolanusse.com\/en\/android-findviewbyid-vs-injectview\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.andreanolanusse.com\/en\/"},{"@type":"ListItem","position":2,"name":"Android findViewById vs @InjectView"}]},{"@type":"WebSite","@id":"https:\/\/www.andreanolanusse.com\/en\/#website","url":"https:\/\/www.andreanolanusse.com\/en\/","name":"Andreano Lanusse | Technology and Software Development","description":"Where Andreano Lanusse talk about technology, software development, programming techniques, databases, games and more through articles, tutorials and videos","publisher":{"@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.andreanolanusse.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":["Person","Organization"],"@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/b51fdf99c01fcd6ae0a5ae894c23837b","name":"Andreano Lanusse","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/49ab23ef70c249c0cb3469f14ef07edc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/49ab23ef70c249c0cb3469f14ef07edc?s=96&d=mm&r=g","caption":"Andreano Lanusse"},"logo":{"@id":"https:\/\/www.andreanolanusse.com\/en\/#\/schema\/person\/image\/"},"description":"Andreano Lanusse is an expert and enthusiastic on software development industry, at Embarcadero he is focused on helping to make sure the products being developed meet the expectations of Embarcadero's customers, as well as defining market strategies for Latin America. Today as Latin Lead Evangelist he spends great deal of time in developer conferences, tradeshows, user group, and visiting customers throughout Latin America. Before Embarcadero, he worked 13 years for Borland, Andreano has worked as Support Coordinator, Engineer, Product Manager, including Product Line Sales Manager, where was responsible to manage the relationship with Brazil developer community, also has worked as Principal Consultant for Borland Consulting Services on the development and management of critical applications. He previously served as Chief Architect for USS Solu\u00e7\u00f5es Gerenciadas (now USS Tempo). Andreano holds a bachelor's degree in Business Administration Marketing Emphasis from Sumare Institute, MBA in Project Management from FGV, certification in Microsoft products, all Borland ALM products, and all CodeGear product line.","sameAs":["http:\/\/www.andreanolanusse.com","https:\/\/x.com\/andreanolanusse"]}]}},"_links":{"self":[{"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":0,"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.andreanolanusse.com\/en\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}