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’s part of RoboGuice and eliminate this issue and make your code cleaner, smaller and replace the use of findViewById, let’s see how it works.

Let’s assume you defined the following edit text:

We would use the following code to access the edit text using findViewById.

In order to use @InjectView we have to extend from RoboActivity class (download the RoboGuice from here), 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.

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.

I wanted to know that if my MainActivity Class is extending from AppCompatActivity Class or any other class which is mandatory for me to extend, then in that case, I cant extends MainActivity to RoboActivity ….. Can you please tell me “How to use @InjectView in my MainActivity if i am not extending to RoboActivity..?”