-
Notifications
You must be signed in to change notification settings - Fork 829
Your First View Injection
To inject your first view into an activity, you'll need to:
- Inherit from RoboActivity
- Set your content view
- Annotate your views with
@InjectView
Injecting views into your RoboGuice activities is simple. First, create a new Activity that inherits from RoboActivity:
public class MyActivity extends RoboActivity {
...
}
Then add a layout to the activity:
// Override onCreate() and call setContentView()
public class MyActivity extends RoboActivity {
@Override
protected void onCreate( Bundle savedState ) {
setContentView(R.layout.myactivity_layout);
}
}
Assuming that the myactivity_layout.xml
file contains a TextView with id text1
, you can now inject that view into your activity and use it without having to call findViewById()
public class MyActivity extends RoboActivity {
@InjectView(R.id.text1) TextView textView;
@Override
protected void onCreate( Bundle savedState ) {
setContentView(R.layout.myactivity_layout);
textView.setText("Hello!");
}
}
That's it! You've injected your first view!
The @ContentView
annotation can be used to further alleviate development of activities and replace the setContentView
statement :
@ContentView(R.layout.myactivity_layout)
public class MyActivity extends RoboActivity {
@InjectView(R.id.text1) TextView textView;
@Override
protected void onCreate( Bundle savedState ) {
textView.setText("Hello!");
}
}
View injection doesn't end with TextViews of course. You can inject any kind of view that you want, even custom views. The only constraint of course is that the type of view you inject must agree with the type in the layout.
There's no reason you have to stick with XML layouts either. You can also inject views for activities that have their content views constructed manually using java code.
Views can also be injected into Fragments & Views as well.
When using Fragments, View injection happens during onViewCreated()
. So after super.onViewCreated()
is called, you can start using your injected views. For example:
@InjectView TextView commentEditText;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
commentEditText.setText("Some comment");
}
- For Projects Using Gradle
- For Projects Using Maven
- Manual Installation for Older Projects
- Inheriting from RoboGuice Classes
- Upgrade Instructions from RoboGuice 2
- Your First View Injection
- Your First Resource Injection
- Your First System Service Injection
- Your First POJO Injection
- Singletons and ContextSingletons (wiki/Understanding Scopes)
- Your First Custom Binding
- Your First Injected Fragment
- Your First Injected Service and BroadcastReceiver
- Your First Testcase
- Your First Injection into a Custom View class
- Your First Injected ContentProvider
- Using Events in your RoboGuice application
- Logging via Ln
- RoboGuice Standard Injections
- How Injection Works
- When Injection Just Works, and when you have to call injectMembers()
- The Difference between Global and Context-scoped Injection
- Analyzing a Guice Stack Trace
- What's the difference between Nullable and Optional?
- RoboBlender wiki
- Use ProGuard with RoboGuice
- Define advanced custom bindings
- Remove or replace RoboGuice's default bindings
- Use your own BaseActivity with RoboGuice
- Inject into an object that you don't instantiate
- Dealing with Circular Dependencies
- Work with Library Projects
- Deal with ComputationException due to StackOverflowError
- Taming Fragmentation using RoboGuice