In the first post about libraries in Android, I proposed two different solutions to share a project with resources but without sharing the source code:
In this post, I will show in detail the second one: to create the resources programmatically instead of using the XML files and export it to a JAR file. The steps are the following:
- Create a java project to add the code.
- Import the Android SDK into the java project.
- Create the resources programmatically.
- Export the java project to a .JAR file excluding the source code.
- Import the .JAR into the client project or add an intermediate Android Library Project.
Let’s show a short example, the same example used in the part II. Suppose we want to create a library to provide a log-in screen to authenticate users in our system. We want to keep the code that connects to our servers and authenticates the users in private. We also want to provide the user interface in order to maintain our own corporative design and now we will keep that user interface in private too.
1. Create the Java project
Create a Java project and create a package and a class in it. Let’s name the class “Authenticator”, a final class with one static method. This class has the code of our authentication system.
Add the method code:
package belencruz; public final class Authenticator { /** * Method to authenticate users. * @param user User name * @param pass User password * @return true if login is correct */ public static boolean authenticate (String user, String pass) { // Connect to my server to authenticate // For this example: if(user.equals("Belen") && pass.equals("belen")) return true; return false; } }
2. Import the Android Jar
Add the Android library to the project as you would do to add any external library .jar. You will find de android.jar file in the Android installation from your system, for example in a path like /androidSDK/platforms/android-18/android.jar.
3. Create the resources
We are creating the resources programmatically, that’s why we added the android.jar in the previous step. Create a new final class named as “LayoutCreator”. In this class we add the method that will contain the code to create the login layout. We can add different methods for different views. Additionally, we will have to create the R.java class to store the IDs of the layout components.
package belencruz; public final class LayoutCreator { /** * Method to create the login view. * @param context Activity that needs the view * @return login View */ public static View createLoginLayout(final Activity context) { LinearLayout loginLayout = new LinearLayout(context); loginLayout.setOrientation(LinearLayout.VERTICAL); ... return loginLayout; }
Now just the code to create the components inside the layout is left. To create the text inputs, add the following code:
// Edit Text User EditText userInput = new EditText(context); userInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); userInput.setId(R.id.userInput); userInput.setHint("e-mail"); // Edit Text Password EditText passwordInput = new EditText(context); passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); passwordInput.setId(R.id.passwordInput); passwordInput.setHint("password"); loginLayout.addView(userInput); loginLayout.addView(passwordInput);
Lines 13 and 14 add the components to the login layout. To create the accept button add this code:
// Button Accept Button loginButton = new Button(context); loginButton.setText("Accept"); loginButton.setId(R.id.loginButton); loginLayout.addView(loginButton);
And finally we need to handle the button event. We get the user credentials from the layout and start the authentication process. Since the login is a costly process, we execute it in a new thread. To simplify the example, I used a Runnable object, a better way would have been the use of an AsyncTask.
loginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new Runnable() { public void run() { TextView user = (TextView) context.findViewById(R.id.userInput); TextView password = (TextView) context.findViewById(R.id.passwordInput); Authenticator.authenticate( user.getText().toString(), password.getText().toString()); } }).start(); } });
Export the project to a .JAR file excluding the source code. If you are using eclipse, then in the export dialog, just check the “Export generated class files and resources” option, but don’t check the option “Export Java source files and resources“.
4. Create an Android Library Project
Create a new Android project and check it as a library project.
Import our library .JAR from the previous step.
Open the main activity and in the onCreate method we need to change the view that is loaded from this activity. Change the call to the setContentView method to this:
setContentView(LayoutCreator.createLoginLayout(this));
5. Create the client Android project
Create a new Android project, not library this time. Add the library project to this new one.
In the main layout of this project, add a login button. When the user clicks the button, the user interface will switch to the login layout from our library. So, in the main activity, add the following code in the handler method of the login button:
public void onLoginClick(View v){ Intent i = new Intent(this, com.belencruz.mylibrary.MainActivity.class); startActivity(i); }
Finally, don’t forget to declare the library activity in the manifest file of the client project. Open the AndroidManifest.xml from the client project and inside the application tag, add the following declaration:
<activity android:name="com.belencruz.mylibrary.MainActivity"> </activity>
hi belen, can i get this full source code?, i need it for my project,