Book Image

Android Studio Cookbook

By : Mike van Drongelen
Book Image

Android Studio Cookbook

By: Mike van Drongelen

Overview of this book

This book starts with an introduction of Android Studio and why you should use this IDE rather than Eclipse. Moving ahead, it teaches you to build a simple app that requires no backend setup but uses Google Cloud or Parse instead. After that, you will learn how to create an Android app that can send and receive text and images using Google Cloud or Parse as a backend. It explains the concepts of Material design and how to apply them to an Android app. Also, it shows you how to build an app that runs on an Android wear device. Later, it explains how to build an app that takes advantage of the latest Android SDK while still supporting older Android versions. It also demonstrates how the performance of an app can be improved and how memory management tools that come with the Android Studio IDE can help you achieve this. By the end of the book, you will be able to develop high quality apps with a minimum amount of effort using the Android Studio IDE.
Table of Contents (17 chapters)
Android Studio Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating your first app called Hello Android Studio


After downloading Android Studio, install it and go through the setup wizards. The wizard checks for some requirements, whether the Java Development Kit (JDK) is available, and other important elements that the installation wizards guide you through.

Once the installation is complete, it is time to develop your first Android app using Android Studio, just to check whether everything has been installed correctly and works the way it should. It probably will be no surprise that this is where the Hello Android Studio recipe comes in.

Getting ready

To go through this recipe, you will need a running Android Studio IDE, an Android Software Development Kit (SDK), and an Android device. No other prerequisites are required.

How to do it...

Let's create our first Android app using Android Studio to check whether everything works fine with the help of the following steps:

  1. Start Android Studio. The Welcome to Android Studio dialog will be shown to you after a few seconds.

  2. Select the Start a new Android Studio project option. Then, the Configure your new project dialog appears.

  3. For Application name, enter HelloAndroidStudio; and for the Company domain field, enter packtpub.com (or use the domain name of your own company if you prefer to do so).

  4. Package names such as packtpub.com and helloandroidstudio are suggested and updated while you type. If you wish, you can edit the Project location before you click on the Next button.

  5. In the Target Android Devices dialog box, check the Phone and Tablet option. Leave the other options unchecked. We will create some of those other interesting targets, such as an Android Wear app, later. For the Minimum SDK, choose API 14. If that one is not (yet) available, click on any of the other available SDKs. We will install more SDKs later. Click on the Next button to continue.

  6. In the next dialog box, Add an activity to Mobile, choose the Blank Activity option and click on the Next button.

  7. The final dialog Customize the activity will be displayed after this. Leave all the values the way they are and click on the Finish button.

  8. Android Studio is now going to create this new app for you. After a while, the project view, a MainActivity class, and an activity_main.xml layout are displayed. If you change the perspective of the project view on the left-hand side of your Android Studio by clicking on the button, that displays the little green Android guy and the text that reads Android, from Android to Project, the layout will look a little bit more like you are used to, that is, if you have used Eclipse before.

  9. Double-click on the app folder to expand it. You will notice a file called the build.gradle file (note that this file also exists on the root level).

  10. Double-click on the build.gradle file to open it and have a look at the values for compileSdkVersion, minSdkVersion, and targetSdkVersion. By default, the compileSdkVersion value is always related to the latest (available) SDK. The value for minSdkVersion is the one that you have chosen in the Target Android devices dialog box.

    Note

    If you want, use a different SDK to compile against; you must change the value for compileSdkVersion. The version you choose might need to be installed first. If you are happy with the current configuration, go to step 14 right away.

  11. If you want to check which SDKs are installed, go the Tools option from the main menu and choose Android from the SDK Manager submenu.

  12. The Android SDK Manager dialog box displays which SDKs are installed. If you need to install a different SDK, you can check the elements you need and click on the Install n packages… button.

  13. After installing the SDKs that you need and having configured your build.gradle file, you can now run your app.

  14. If you are using a physical device for Android development, you need to unlock the developer options first. On your device, start the Settings app and go to the Device info option. (This option may be on the General tab or section or at another place, depending on the version and flavor of Android that your device is running on).

    Note

    If you do not have a real device, I strongly recommend you get one as soon as possible. To get started, you can use an emulator for now. You can use the emulator that comes with the Android SDK or you can read the recipe about Genymotion first to find out how to use emulated devices.

  15. In the Device Info view, scroll all the way down until you see the Build number option. Now, click seven (7) times on Build number to unlock (enable) the developer mode. (No, this is not a joke). You now have the developer's menu unlocked.

    Note

    On older Android versions (below 4.2), this step may be skipped, or if the developer options are already available as a menu item in the settings app, this step may be skipped.

  16. Now that you have got a new option in your Settings app, called Developer options, click on it and have a look at it. It is important that you enable the USB debugging option within this menu. In addition, you might want to enable or disable some of the other options.

  17. Connect your device and run your app from Android Studio by clicking on the green triangle next to the drop-down box that reads the app. Or, choose the Run... option from the Run menu. Then, the Choose Device dialog box appears. Your device should now appear in the list of the Choose a running device option. ( If your device does not appear in the list, reconnect your device).

  18. Click on the OK button. (For Windows, before you are able to connect your device, it is often necessary to install a driver first.)

  19. On your device, a dialog box may pop up, requiring you to accept the finger print. Choose Allow in order to continue.

The app is now being deployed on your device. If everything goes well, your new app is now shown on your device that says Hello world! Hurrah! I admit this is not really a very exciting app, but at least we know now that Android Studio and your device have been configured correctly.

How it works...

Android Studio will take care of the basic parts of your app setup. All you need to do is choose the target and minimal SDK for your app. Using the API level 14 (Android 4.0) is currently the best option, as this will allow your app to run on most Android devices.

The app will be compiled against the chosen (compile) SDK by Android Studio.

The app will be copied to your device. Having the USB debugging option enabled will help you troubleshoot any issues, as we will find out later.