{"id":17189,"date":"2015-02-09T13:03:31","date_gmt":"2015-02-09T07:33:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17189"},"modified":"2016-12-19T14:54:51","modified_gmt":"2016-12-19T09:24:51","slug":"introduction-to-roboguice-with-a-brief-example","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-roboguice-with-a-brief-example\/","title":{"rendered":"Introduction to RoboGuice with a brief example"},"content":{"rendered":"<p>Have you heard about\u00a0RoboGuice? Well, don&#8217;t be surprised. It is now getting familiar like other things in Android development.<\/p>\n<p>RoboGuice uses Google Guice library, a simple framework and easy-to-use dependency injection to Android.<\/p>\n<p>Just think about casting a view using findViewById() and it is so necessary, for which you have a one new thing i.e. RoboGuice. RoboGuice gives you a full pleasure to stay out of this in your Android development experience and makes things simple and fun. May be possible, you always forget to check null for getIntent().getExtra(). RoboGuice will help you.<\/p>\n<p>No more guesses in development, that you have to inject your View, Resource, System Service, or any other object, and let RoboGuice take care for all whatever you want.<br \/>\n&#8220;There\u0092 is no \u0093magic&#8221;\u0094. The thing you want is configured explicitly by RoboGuice or can be overridden by you.<\/p>\n<p>Just have a look on some code, you will get the idea. Here is the example of an activity.<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nclass RoboExampleActivity extends Activity { <\/p>\n<p>\tTextView txtName;<br \/>\n        ImageView imgProfile;<br \/>\n        LocationManager loc;<br \/>\n        Drawable icon;<br \/>\n        String userName;<\/p>\n<p>        public void onCreate(Bundle savedInstanceState) {<br \/>\n            super.onCreate(savedInstanceState);<br \/>\n            setContentView(R.layout.main);<\/p>\n<p>            txtName      = (TextView) findViewById(R.id.txt_name);<br \/>\n            imgProfile = (ImageView) findViewById(R.id.img_profile);<br \/>\n            loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);<br \/>\n            icon      = getResources().getDrawable(R.drawable.ic_launcher);<br \/>\n            userName    = getString(R.string.app_name);<br \/>\n            txtName.setText( &quot;Hello, &quot; + userName);<br \/>\n        }<br \/>\n    } <\/p>\n<p>[\/sourcecode]<\/p>\n<p>This example has 19 lines of code. If you are trying to read from onCreate(), you have to skip over 5 lines of code initialization to find the only thing that matters: txtName.setText(). And complex activities can end up with a lot more of this sort of initialization code.<\/p>\n<p>Compare this to the same app, written using RoboGuice:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n @ContentView(R.layout.main)<br \/>\n    class RoboExampleActivity extends RoboActivity {<br \/>\n        @InjectView(R.id.txt_name)             \tTextView txtName;<br \/>\n        @InjectView(R.id.img_profile)        \tImageView imgProfile;<br \/>\n        @InjectResource(R.drawable.ic_launcher) Drawable icon;<br \/>\n        @InjectResource(R.string.app_name) \tString userName;<br \/>\n        @Inject                            \tLocationManager loc; <\/p>\n<p>        public void onCreate(Bundle savedInstanceState) {<br \/>\n            super.onCreate(savedInstanceState);<br \/>\n            txtName.setText( &quot;Hello, &quot; + userName);<br \/>\n        }<br \/>\n    }<br \/>\n[\/sourcecode]<\/p>\n<p>Did you notice any thing? Did you try to compare?<\/p>\n<p>&#8230;..<br \/>\n&#8230;&#8230;..<\/p>\n<p>Let me help you. In this example, onCreate() is much easier to understand\u00a0at a glance. All the platform extra work is discarded and you have just your own app&#8217;s main business logic. If you need a SystemService? Inject it. if you need a View or any Resource? you can Inject those, too, and RoboGuice will do all.<\/p>\n<p>RoboGuice&#8217;s main objective is to make your code only you app, rather than be about all the initialization and life cycle code you typically have to maintain in Android.<\/p>\n<p><strong>Installation<\/strong><br \/>\nTo use RoboGuice, you need to download following JAR files and add them to your classpath:<\/p>\n<p>http:\/\/repo1.maven.org\/maven2\/org\/roboguice\/roboguice\/2.0\/roboguice-2.0.jar http:\/\/repo1.maven.org\/maven2\/com\/google\/inject\/guice\/3.0\/guice-3.0-no_aop.jar <a href=\"http:\/\/repo1.maven.org\/maven2\/com\/google\/code\/findbugs\/jsr305\/1.3.9\/jsr305-1.3.9.jar\">http:\/\/repo1.maven.org\/maven2\/com\/google\/code\/findbugs\/jsr305\/1.3.9\/jsr305-1.3.9.jar<\/a><\/p>\n<p>As I mentioned earlier that RoboGuice provides all classes for using fragments and actionbar<br \/>\nIt also available with sherlock action bar.<\/p>\n<p>Get all related classes from <a title=\"Click here to know more and get all the related classes to move on towards RoboGuice!!!\" href=\"https:\/\/github.com\/roboguice\/roboguice\">GitHub<\/a><br \/>\nHave a nice time with RoboGuice. \ud83d\ude09<\/p>\n<p>Anand Rai<br \/>\nMy Linkedin<br \/>\n<a title=\"My Stackoverflow\" href=\"http:\/\/stackoverflow.com\/users\/2021453\/anandroid\">My Stackoverflow<\/a><br \/>\n<a title=\"My Twitter\" href=\"https:\/\/twitter.com\/rai_anandroid\">My Twitter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you heard about\u00a0RoboGuice? Well, don&#8217;t be surprised. It is now getting familiar like other things in Android development. RoboGuice uses Google Guice library, a simple framework and easy-to-use dependency injection to Android. Just think about casting a view using findViewById() and it is so necessary, for which you have a one new thing i.e. [&hellip;]<\/p>\n","protected":false},"author":131,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[518],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17189"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17189"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17189\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}