Android findViewById vs @InjectView

Let's share the knowledge with your friends
  • 9
  • 3
  •  
  •  
  •  
  •  
  •  
  •  
    12
    Shares

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.

class Main extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        EditText firstname = null;
        firstname = (EditText) findViewById(R.id.fistnameEditText);
        firstname.setText( "My first name is Barack"); 
    } 
}

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.

class Main extends RoboActivity { 
     @InjectView (R.id.firstnameEditText)      EditText firstname; 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        firstname.setText( "My first name is Barack" ); 
    } 
}

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.

class Main extends RoboActivity { 
     @InjectView (R.id.firstnameEditText)  EditText firstname; 
     @InjectView (R.id.lastnameEditText)   EditText lastname; 
     @InjectView (R.id.photoImage)         ImageView photo; 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        firstname.setText( "My first name is Barack" ); 
        lastname.setText( "My last name is Obama" ); 
        photo.setImageBitmap( null ); 
    } 
}

Hope this small tip can help you.


Let's share the knowledge with your friends
  • 9
  • 3
  •  
  •  
  •  
  •  
  •  
  •  
    12
    Shares
4 replies
  1. Vivek
    Vivek says:

    Hi andreanolanusse,

    Great Article to learn. 🙂

    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..?”

    Reply
  2. Vaikesh K P
    Vaikesh K P says:

    Hi andreanolanusse,

    It’s a nice work, But i think you missed a small point I think so.

    I think there is a need of this :

    ButterKnife.inject(this);

    below your setContentView(R.layout.main);

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Specify Facebook App ID and Secret in Super Socializer > Social Login section in admin panel for Facebook Login to work

Specify Twitter Consumer Key and Secret in Super Socializer > Social Login section in admin panel for Twitter Login to work

Specify LinkedIn Client ID and Secret in Super Socializer > Social Login section in admin panel for LinkedIn Login to work

Specify GooglePlus Client ID and Secret in Super Socializer > Social Login section in admin panel for GooglePlus Login to work

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