Reputation: 5415
I've got a few projects Im trying to combine into one and Ive already included all the other projects as library ones through the settings. What Im sort of stuck on is how to use them in my main activity.
Ive messed around and feel like Ive almost got it but Im just stuck. I know I have to declare all the new activities in the main manifest but Im not sure about what to include in the main .java activity inorder to call from the newly included stuff.
Is this something that I could figure out by looking at the android api demos that come with the sdk?
Can some one point me to an open source project somewhere and explain the process used to include the library projects.
Any help would be much appreciated.
Upvotes: 2
Views: 5789
Reputation: 1643
Are you using eclipse to manage your settings? It doesnt really matter, but its easier to make sure your library paths are set correctly and are accessible by the calling project.
Assuming that they are accessible, you just access the classes in the library like any other class: Import the package and instantiate the class as you would normally. For an android activity, this means that you would likely create an activity from the library based on some response from your main activity. It doesn't matter if that Activity is local to this project or imported from a library. eg:
// import the activity/package/class from your library
import com.mylibrary.activities.ImportedActivity;
public class LocalActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// Button Code
button = (ImageView) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// create a new intent based on your library activity
Intent myIntent = new Intent(v.getContext(), ImportedActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
note, I have not tried to compile the code above, its just for purposes of demonstration.
If your libraries are correctly referenced in eclipse, this should work. If not you will get errors on either the import of the external libraries (package not found) or build errors when the actual library is needed.
Upvotes: 4