RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google’s own Guice library.
The first you download
roboguice-2.0.jar
guice-3.0-no_aop.jar
jsr305-1.3.9.jar
Add the required libraries to the libs folder of you new android project.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".TheDeveloperWorldIsYours" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
package com.thedeveloperworldisyours.inject; import com.google.inject.Inject; import android.os.Bundle; import android.view.Menu; import roboguice.activity.RoboActivity; import roboguice.inject.InjectResource; import roboguice.inject.InjectView; import android.location.LocationManager; import android.widget.TextView; public class TheDeveloperWorldIsYours extends RoboActivity { @InjectView(R.id.text) TextView name; @InjectResource(R.string.app_name) String myAppName; @InjectResource(R.string.action_settings) String myActionSettings; @InjectResource(R.string.hello_world) String myHelloWorld; @Inject LocationManager loc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the_developer_world_is_yours); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.the_developer_world_is_yours, menu); return true; } }