Win Free copy of my book on Android Studio

Readers would be pleased to know that I have teamed up with Packt Publishing  to organize a Giveaway of the Android Studio Application Development book.

And three lucky winners stand a chance to win ecopy of their new book. Keep reading to find out how you can be one of the Lucky Winners.

Overview

Android Studio Application Development

  • Install Android Studio and configure the Android Software Development Kit
  • Create your first project and explore its structure
  • Improve your productivity while programming, by getting the best from the code editor
  • Design the user interface and learn how to handle user events
  • Integrate the Google Play Services into your app
  • Test your app using the emulator and learn how to manage the virtual devices
  • Run and debug your app
  • Monitor your app whilst it is running and improve its performance
  • Build your app for release

How to Enter?

All you need to do is write your e-mail and name below. It’s that simple. Winners will be contacted by email, so be sure to use your real email address when you comment!

In the second step, you can get extra entries if:

  • You head on over to the book page (Android Studio Application Development) and look through the product description of the book and drop a line via the comments after sending your email, to let us know what interests you the most about this book.
  • You tweet about this giveaway.
  • You follow me on twitter.

Winners from the U.S. and Europe can either choose a physical copy of the book or the eBook. Users from other locales are limited to the eBook only.

Read More

(Private) Libraries in Android: Part III

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:

  1. Create a java project to add the code.
  2. Import the Android SDK into the java project.
  3. Create the resources programmatically.
  4. Export the java project to a .JAR file excluding the source code.
  5. Import the .JAR into the client project or add an intermediate Android Library Project.

Private library process

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.

Java project structure

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.

android.jar in project

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.

Library structure

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.

Library project check

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.

Add the library project

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>

Read More