Usage of RoboGuice library:
- Views Injection:: To initialize views, use @InjectViews, for example: @InjectView(R.id.textView1) TextView textView1;
- Resources Injection: To initialize and get resources, use @InjectResources, for example: @InjectResource(R.string.app_name) String name;
- System services Injection: To initialize and access system services, use @Inject, for example: @Inject LayoutInflater inflater;
- POJO object Injection: To inject and initialize POJO object, use @Inject, for example: @Inject Foo foo;
General Activity :
public
class
TestActivity
extends
Activity{
TextView textView1;
TextView textView2;
ImageView imageView1;
String name;
Drawable icLauncher;
LocationManager locManager;
LayoutInflater inflater;
NotificationManager notifyManager;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
imageView1 = (ImageView) findViewById(R.id.imageView1);
name = getString(R.string.app_name);
icLauncher = getResources().getDrawable(R.id.ic_launcher);
locManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
notifyManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
textView1.setText(
"Hello World! RoboGuice demo"
);
}
}
Let’s check the magic of RoboGuice and let’s slim down the code.
public class TestActivity extends RoboActivity{
@InjectView(R.id.textView1) TextView textView1;
@InjectView(R.id.textView2) TextView textView2;
@InjectView(R.id.textView1) TextView textView1;
@InjectView(R.id.textView2) TextView textView2;
@InjectView(R.id.imageView1) ImageView imageView1;
@InjectResource(R.string.app_name) String name;
@InjectResource(R.drawable.ic_launcher) Drawable icLauncher;
@Inject LocationManager locManager;
@Inject LayoutInflater inflater;
@Inject NotificationManager notifyManager;
@Override protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
textView1.setText(name);
}
}
Installation
To use RoboGuice, you need to download following JAR files and add them to your claspath:
To use RoboGuice, you need to download following JAR files and add them to your claspath:
No comments:
Post a Comment