Introduction to RoboGuice with a brief example

09 / Feb / 2015 by Anand Rai 0 comments

Have you heard about RoboGuice? Well, don’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. 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.

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.
“There’ is no “magic””. The thing you want is configured explicitly by RoboGuice or can be overridden by you.

Just have a look on some code, you will get the idea. Here is the example of an activity.

[sourcecode language=”java” wraplines=”false” collapse=”false”]
class RoboExampleActivity extends Activity {

TextView txtName;
ImageView imgProfile;
LocationManager loc;
Drawable icon;
String userName;

public void onCreate(Bundle savedInstanceState) {

txtName = (TextView) findViewById(;
imgProfile = (ImageView) findViewById(;
loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
icon = getResources().getDrawable(R.drawable.ic_launcher);
userName = getString(R.string.app_name);
txtName.setText( "Hello, " + userName);


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.

Compare this to the same app, written using RoboGuice:

[sourcecode language=”java” wraplines=”false” collapse=”false”]
class RoboExampleActivity extends RoboActivity {
@InjectView( TextView txtName;
@InjectView( ImageView imgProfile;
@InjectResource(R.drawable.ic_launcher) Drawable icon;
@InjectResource(R.string.app_name) String userName;
@Inject LocationManager loc;

public void onCreate(Bundle savedInstanceState) {
txtName.setText( "Hello, " + userName);

Did you notice any thing? Did you try to compare?


Let me help you. In this example, onCreate() is much easier to understand at a glance. All the platform extra work is discarded and you have just your own app’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.

RoboGuice’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.

To use RoboGuice, you need to download following JAR files and add them to your classpath:

As I mentioned earlier that RoboGuice provides all classes for using fragments and actionbar
It also available with sherlock action bar.

Get all related classes from GitHub
Have a nice time with RoboGuice. 😉

Anand Rai
My Linkedin
My Stackoverflow
My Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *