Android Studio Application Development

Android Studio Application Development is the book I’ve been working on during this last summer. Last week was published by Packt Publishing. It is available at several sites:

Book coverBook backside

Mobile apps have become extremely popular over the last few years and offer a great opportunity for developers. Android is now one of the most popular mobile systems in the world. The new Android Studio IDE has introduced a specialized development environment, welcomed by the emerging Android community, which is perfect for the development of apps.
“Android Studio Application Development” is a practical guide that will show you how to develop and build Android apps using this new, integrated development environment. Following a tutorial approach, this book will progress from the basics features up until the steps to build for release, using real world examples. This book is a must have for every Android app developer evangelist.

Our book explores the Android Studio development environment in-depth, taking you through the necessary steps to create an app, test it, and prepare it for release. First, you will learn how to use the code editor in order to increase your productivity, and to easily create the screens of your app. Then, this book will show you how to debug your app and examine its performance using the most important additional tools within Android Studio: Android SDK tools. We will also take a look at the existing Google Play Services and how they can be integrated into your app.

Finally, it is important for a developer to be able to search for any help they might need. This book provides a vast help section which covers online documentation, updates, and FAQs.

Sample Chapter

You can download a sample chapter here.

Read More

(Private) Libraries in Android. Part II

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 first one: creating a Library Project to include the resources, but including the source code using a JAR file. The steps are the following:

  1. Create a java project to add just the code, not resources.
  2. Export the java project to a JAR file excluding the source code.
  3. Create a Library Project to include the resources.
  4. Add to the Library Project the previous created JAR.
  5. The client project will import the Library Project which contains the resources and the JAR file.

Private library process

Let’s show a short example. 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.

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.

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;
   }
}

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“.

2. 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. The project structure now looks like this:

Library project structure

Open the main layout and create a simple login layout. To avoid conflicts between names, it is a good idea to change the name of this layout file, for example, to “library_activity_main.xml”.

Login layout

Now open the main activity and add the method that will be run when the user clicks on the Accept button. Add the code to call the method of our .JAR:

import belencruz.Authenticator;
...

/**
* When user clicks the Accept button. Login.
* @param v
*/
public void onAcceptClick(View v) {
   final EditText et_user = (EditText) findViewById(R.id.editText_user);
   final EditText et_pass = (EditText) findViewById(R.id.editText_pass);

   new Thread(new Runnable() {
      public void run() {
         Authenticator.authenticate(
            et_user.getText().toString(),
            et_pass.getText().toString());
      }
   }).start();
}

In this method, 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.

The project library is now ready to be imported in other Android projects.

3. 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