Android For Beginners. Developing Apps Using Android Studio - Barbara Hohensee - ebook
Opis

The objective of this book is to give an insight into Android programming based on practical App projects. The Apps have different core focuses and hence one can extend and develop them further based on his/her Android knowledge. Thanks to active programming, the users will be quickly acquainted with the work environment and will learn how to solve problems in Android step-by-step. Android Studio, which is a completely new development environment, will be used for programming. 

Ebooka przeczytasz w aplikacjach Legimi na:

Androidzie
iOS
czytnikach certyfikowanych
przez Legimi
Windows
10
Windows
Phone

Liczba stron: 205

Odsłuch ebooka (TTS) dostepny w abonamencie „ebooki+audiobooki bez limitu” w aplikacjach Legimi na:

Androidzie
iOS

Android for beginners. Developing apps using Android Studio

Barbara Hohensee

––––––––

Translated by Amit Dilip Dharma 

“Android for beginners. Developing apps using Android Studio”

Written By Barbara Hohensee

Copyright © 2014 Barbara Hohensee

All rights reserved

Distributed by Babelcube, Inc.

www.babelcube.com

Translated by Amit Dilip Dharma

“Babelcube Books” and “Babelcube” are trademarks of Babelcube Inc.

Table of Contents

Title Page

Copyright Page

Android for beginners | Developing Apps with Android Studio | Barbara Hohensee | First edition

Preface

Target group of this book

Requirements for the computer

Android telephone or tablet

Java knowledge

How this book is structured?

About Android Studio

Introduction | General information

Constituents of an app

First screen

The second screen that would be shown after clicking the 3rd element from the list

Installing Java and Android Studio

For installation | Java installation

Java SE Development Kit 7u40

;C:Files.7.0_40 | Testing the Java installation

Installing Android Studio

Configuring Android Studio

Android SDK Manager

First project

Project structure

Which files are stored in the project structure, and where?

The Hello World app

Adding the ActionBar to the app

Adding a button to the ActionBar

Adding comments | Comments in Java files

Comments in Android XML files

Setting up the emulators | Installing the default emulator from the Android SDK

Setting up new emulators

Installing and setting up an external emulator | Genymotion

Connecting a telephone of a tablet | Running the app on a telephone or a tablet.

Building and running the app

Running the app from the command line

The significance of AndroidManifest.xml and build.gradle

The build.gradle file

Overview of IDE | Work space

Editor | Configuring the editor

Tips and tricks for the editor

Rename

Generating strings on the fly

App layout design: text / design editor

Second project: DinoQuiz trivia game

The app idea

Creating the layout for the first screen

Assigning functions to buttons

Life cycle

Edit the QuizActivity.java activity as well

Activities

A new class will be created

A colourful end result

The third project: Android Easter Eggs

ListView

Example: The Easter Egg image for Android KitKat

Creating the ListViews layout

Add elements to the ListView

Creating the data source

Creating the adapter

Establishing a connection between the adapter and ReaderActivity

Creating a new screen

Creating the second screen

Connecting both screens with each other

GridView

Creating layouts for the GridView

Create an image adapter

Creating an activity for the GridView

GridLayout

The fourth project: BMI calculator

Setting up a so-called spinner

The fifth project: True or False

The sixth project: WebView & Co.

Internal Android browser

Adding the Media Player

Life cycle

Logcat (system log)

Integrating the websites of AppEngine or GitHub Pages

Using the WebKit

HTML code in strings.xml and TextView

The seventh project: Survey form

Share intents

The eighth project: | Graphics, drawing and animation

Drawing in a view

Example for drawing in a view

Drawing in the SurfaceView

OpenGL ES

Tween animation

The ninth project: Fragments | What are Fragments

Activity with ListView | Activity with details for the Meine Familie (my family) element

Project: Klassentreffen (class reunion)

Creating the 2-pane layout (master/ detail flow)

Project:  German TV on-the-go

Developing for Google TV | What is Google TV?

How to test apps on Google TV

Switching over the adb to a WiFi connection

Configuration in Google TV

Special features in Google TV

The TV is always in the landscape format.

The tenth project: Preferences

What are Preferences

Project: Pinguine auf dem Eis (penguin on ice)

Setting the Preferences page

Creating a layout file

Creating a Java class for preferences

Accessing the Settings screen via ActionBar

The eleventh project:  SQLite database

What is SQLite?

Creating a sample project

Bonus project: Photo Diary

Accessing the database in the emulator via a terminal

Establishing a connection for the emulator at the Shell level

Exporting a SQLite database

Google Play Service SDK

Integration of Google Maps API v2 | Creating an SHA-1 fingerprint

Command to get a debug certificate

Command to get a release certificate

Creating a project in the Google API Console

Creating a new Android Map project

Inserting a map in the app

Using the Google Map template

Sample project

Combining the Android SDK with external SDKs

Wearables

Kindle tablets

Samsung telephones and tablets

Android Wear

Moto 360

The Android Wear Preview app

The zip file | Examples

Preparing the app for the Android market | Google Developer and other accounts

Adding Google Ads to an app

Screenshots of the app

Signing

ProGuard

Signing a release APK with Gradle

Annexe | Gradle build system

The default Gradle file for an Android project

GameEngines with Android Studio

AndEngine

libGDX game engine

Using Version Control (VCS)

Configuring the VCS for the current project

Product Flavours - Build Types - Build Variants

Links

Imprint

Your Review and Word-of-Mouth Recommendations Will Make a Difference

Are You Looking For Other Great Reads?

Android for beginners

Developing Apps with Android Studio

Barbara Hohensee

First edition

Preface

The objective of this book is to give an insight into Android programming based on practical app projects. The apps have different objectives and target groups and hence one can extend and develop them further based on his/her Android knowledge. Thanks to active programming, the users will be quickly acquainted with the work environment and will learn how to solve problems in Android step-by-step. Android Studio, which is a completely new development environment, will be used for programming.

Target group of this book

This book is intended for everyone who is interested in the Android app development and would like to learn the same with DIY examples.

Requirements for the computer

What are the requirements for the computer? You can select any modern operating system of your choice (Windows XP, Windows 7/8, Mac OS X and Linux). You will need Administrator rights for installation.

Android telephone or tablet

Since an emulator is integrated in Android Studio, you can start Android development even without an Android telephone or a tablet. Testing with the emulator makes sense since the app can be tested with all possible Android versions, screen sizes and resolutions. In addition, you can also use specific debugging options in the emulator such as Hierarchy View which can otherwise be used only with a Google developer telephone. However, the emulator has its limitations. For example: One cannot make telephone calls, the options to test the GPS are restricted, the accelerator in the telephone responds to movements; one cannot hold the emulator in hand, which makes it difficult or even impossible to test the games.

If you are not well-versed with an Android device until now, you might ask which would be an ideal choice from a developer’s point of view.

I have given a few pointers here, which will hopefully make the decision making easier.

Which Android version should the telephone use? The latest? Probably yes, when you are thinking of developing an app for the latest version of Android. If you want to develop apps for the masses, you must know the Android versions used by the masses. Based on a test conducted by Google at the end of October 2013, the distribution is as follows:

•   Android 2.2 Froyo API 8 => 1.7%

•   Android 2.3.3 - 2.3.7 Gingerbread API 10 => 26.3%

•   Android 3.2 Honeycomb API 13 => 0.1%

•   Android 4.0.3 - 4.0.4 Ice Cream Sandwich API 15 => 19.8%

•   Android 4.1.x Jelly Bean API 16 => 37.3%

•   Android 4.2.x Jelly Bean API 17 => 12.5%

•   Android 4.3 Jelly Bean API 18 => 2.3%

This means that Android 4.x versions are used by than 70% users. Since the new Android Version 4.4 KitKat has been released, this statistics will soon further improve in favour of Android 4.x and probably get worse for Froyo and Gingerbread.

Screen size and resolution are other factors that can influence the purchase of a developer device. Testing the layout of an app is extremely important. You can imagine that nobody would like an app where the text is not legible or the buttons are too small and one always accidentally clicks on 3 other things when one wants to click on “Next”. My tip: search for the most commonly used screen resolutions in Google. In October 2013, the vast majority of devices had a resolution of 480x800 pixels.

For the latest information, visit:

http://developer.android.com/about/dashboards/index.html

Java knowledge

Knowledge of Java is recommended. However, you can also start with this book and refer to a Java book wherever you face problems due to the lack of Java knowledge. A list of free Java books that are used in schools, colleges and universities is given in the annexe.

How this book is structured?

This book has 3 parts:

Part 1:

The first part deals with the installation and configuration of Java and Android Studio. You will get an overview of the development environment and an insight how the project structure of an Android app looks in Android Studio.

The 1st part concludes with the creation and commissioning of a test app project. This will ensure that Android Studio functions as expected. You would not appreciate if you realise some parts are missing when you are already in the middle of a book, would you?

Part 2:

The second parts deals with the creation of different app projects. 

Each of the apps begins with a plan. The practical implementation of the app idea is then explained. 

Among other things, you will learn the following on the course of completing the apps:

Creating different screen layouts

Supporting different screen sizes

Designing clickable buttons and assigning functions to them

Using and managing the resources

Creating a series of different activities

Creating a database

Adding an ActionBar

Creating animation

Making calculations in the app

Using Google libraries

Working with Google maps

Integrating ads in the app

Providing support for different languages

Preparing the app for the Android market 

Complete source code is provided for each app project; refer to the links.

Part 3:

Advanced sections are included in the third part. These include:

Gradle

Connecting your project with Github

Product Flavours - Build Types - Build Variants

GameEngines AndEngine and libGDX

Links for the source code and more information is given in the annexe. 

About Android Studio

In May 2013, Google presented the new development environment for Android apps during the developers’ conference. The new IDE is intended to replace Eclipse and is based on the "IntelliJ IDEA" Java-IDE by JetBrains.

The powerful Code-Editor with built-in functions such as "Smart Editing", which ensures better legible code, or "Advanced Code Refactoring” is one of the core components of Android Studio.

The Gradle Build System is another novel feature introduced with Android Studio. It replaces the Ant Build System used until now.

Gradle allows developers to create various configurations so that different app versions can be produced using the same code. This is really useful when you want to release a free and a paid version of an app.

Gradle improves the reusability of the code and the integration on a build server.

Similar to Eclipse, the layout of an app can be created either in the text editor or in a graphical interface (design mode) in Android Studio.

The design mode was further improved for Android Studio. The app layout shows the layout for different resolutions, Android versions and country-specific special features in the preview.

A series of new features and services were integrated in Android Studio. The developer console gives developers tips for optimising an app and it also allows uploading the “string files” (strings.xml) to be translated to a central server, and to insert them again in the app after translating them.

Android Studio was also enhanced with an option to add Google Cloud Messaging (CGM) to the project that allows sending messages to the app and receiving the messages from the app on the cloud server.

It is not a new feature to connect an Android app with a cloud server/ App Engine Server app. The integration into an Android app project is the novelty. Earlier, it was essential to create an App Engine Server app and an Android app that is supposed to work with this server app separately from each other.

Knowledge to create an App Engine Server app using Java is essential for using the GCM. This is not explained in this book.

Here are the links of two starter websites for interested users:

• https://developers.google.com/appengine/docs/java/

• http://android-developers.blogspot.se/2013/06/adding-backend-to-your-app-in-android.html

Android Studio has been developed by Google in collaboration with JetBrains and is based on the community version of IntelliJ. JetBrains Java IDE InelliJ supports Android app development since 2 years. Current IntelliJ Version 12 has new features that have been developed in collaboration with Google, but not yet integrated. These will be integrated in Version 13+. Like before, even the new version will support a series of development environments such as Java, Android, Adobe Gaming SDK, Groovy, Scala.

On the other hand, Android Studio will be restricted to the development of Android apps. Google has not specified an official release date for Android Studio.

Android Studio is still being developed and should not be used in the productive app development as yet. However, since a major part of the IDE is already functional, users can still start befriending Android Studio. This is recommended since they can rev up once the finished version is released.

You can download the current version of Android Studios from the following link:

http://developer.android.com/sdk/installing/studio.html

Introduction

General information

Those, who want to start with Android app programming, must simultaneously fight on two fronts. Not only they have to deal with Android programming, but must also learn a new development environment. Naturally, this fact has been taken into account in this book. All projects are exclusively created in Android Studio and individual steps for Android Studio are explained while developing these projects. In addition, this book also has a chapter “Overview of the IDE” which contains a tour through the most important menus of Android Studio. A few decisive changes concerning the development of Android apps were simultaneously made with the introduction of Android Studio. A series of these changes are applicable for the new Gradle Build System. In the sample projects, Gradle is dealt with only to the extent required for the project. A separate chapter named Gradle is included; it contains more information about Gradle and its operation and significance.

Constituents of an app

The app normally comprises a series of different files that can be classified into 4 categories:

• Java classes

• Layout files

• Resources

• Configuration files

We assume that our app two screens - the first shows a list from which individual elements can be clicked and the second shows the result after clicking the element. The app would look as shown below:

First screen

After starting the app, a Java file is first started. This Java file contains the file name of the layout file that needs to be displayed and starts it as a so-called view, where we can see the list on the screen. The layout file contains the design structure (list) and information regarding texts and images that need to be displayed. Texts and images are stored in the resource folders and files. The layout file contains only the links to the resources. The functioning of a layout file is similar to that of a website (HTML).

The Java file started (activity) not only has the function of displaying the fist screen, but it also waits for an event wherein an element from the list is clicked. It reacts to the click and guides the app to the second Java file.

The second screen that would be shown after clicking the 3rd element from the list

A separate layout file is allocated to the second Java file, which is also an activity; the second Java file shows this layout file. The functionality for this app is thus utilised. 

In general, we can say that we need an activity and the associated layout file to display something on the screen. However, not all Java files, which are sometimes a part of an app, comprise activities. This means that not all Java classes display something on the screen. Adapter and databases are other commonly used Java classes. All three types of Java classes are used in different projects mentioned in this book.

Android Studio is used as the primary tool for developing the apps in this book.

Installing Java and Android Studio

If Java SDK and Android Studio have already been installed, you can continue with the “First project” chapter to test the work environment.

If not, a step-by-step guide is given below.

In principle, 3 things are required for the Android development:

• Java SDK

• Android SDK

• An environment for building the apps

You can download the Java SDK for your operating system from Oracle's homepage. The Android SDK is included in Android Studio and need not be separately installed. Eclipse, IntelliJ and Android Studio are normally available as the IDE/ integrated development environment.

Eclipse and IntelliJ, up to version 12, work with the Ant Build System which will no longer be supported 100% by Google. 

The Android Studio development environment uses the Gradle Build System to test and build apps.

The new Android Studio development environment is exclusively used in this book.

For installation

Java installation

Go to Oracle's download page

http://www.oracle.com/technetwork/java/javase/downloads/index.html

click on <JDK download> and then on the latest Java Development Kit on the next page, e.g.

Java SE Development Kit 7u40

When you have decided for a Java version, download the corresponding file depending on your operating system and install it.

Windows users are recommended to set the path for JAVA_HOME as follows:

In the control panel

-> System -> Advanced System Settings -> Advanced -> Environment variable

Click on New and generate a value for the Java environmental variable:

Name: JAVA_HOME 

Value: e.g. C:Files.7.0_40

If CLASSPATH does not exist yet, create it afresh and enter the value depending on the path: C:Files.7.0_40

Under PATH, specify the path depending on the value:

;C:Files.7.0_40

Testing the Java installation

Enter the following command in the terminal window:

java -version

This will show an active Java version, e.g.:

java version "1.6.0_37"

Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)

Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)

Installing Android Studio

Pre-installed and usable Java 1.6 or 1.7 by Oracle is a prerequisite. Android Studio functions better with Java 1.6 in many cases.

Download the installation package from here depending on the operating system used:

http://developer.android.com/sdk/installing/studio.html

Specifically for Windows:

Double-click the downloaded EXE file and follow the instructions.

Specifically for Mac OS:

The installation of Android Studio requires the following system setting:

In System Preferences, go to Security and Privacy, and select Anywhere under “Allow applications downloaded from” option.

Then double-click the downloaded DMG file. Then drag the application icon in the programme folder.

Specifically for Linux:

Unpack the downloaded TGZ file and then copy the folders to the desired location.

Start Android Studio using the studio.sh file that is located in the android-studio/bin/ folder.

Configuring Android Studio

The Android Studio configuration is applicable for all operating systems; naturally, the paths for Java and Android SDK differ.

The latest Android SDK is included in Android Studio. After installation, download additional components depending on the requirement as described in the SDK Manager chapter.

Before starting with a project, check all settings in Android Studio. You can always access and change configuration settings subsequently using the "Project Structure” menu as described below.

After starting Android Studio, click on "Configure" in the welcome screen.

Click on "Project Defaults" in the “Configure menu”.

Click on "Project Structure" in "Project Defaults”.

If the Java SDK and/or the Android SDK do not appear here due to any reason, add these again as follows.

In "Project Structure", first select "SDKs" from the left panel, then click on the (green) "+" in the middle panel and select "JDK".

Then select the directory in which the Java SDK is located. The process establishes a connection between the Oracle Java SDK and Android Studio.

Repeat this process in the next step to connect the Android SDK with Android Studio.

Then select the “Project” menu item from the left "Project Settings" column to define the default platform/ Android version that needs to be used by future Android projects. (Can be done later if required)

Return to “Project Defaults” and “Settings”

and ensure that the correct Java version is enabled.

Android Studio is not now ready. However, starting the SDK Manager and downloading and installing a few additional packages is recommended to be able to use its entire scope right from the beginning.

You can use the SDK Manager to install and update Android platforms, support libraries and Android development tools.

Since Android is being continuously developed, you must regularly check for updates that could be important for your own app development

Android SDK Manager

You can start the SDK Manager using three different methods:

From Welcome screen -> Configure -> SDK Manager

Use the icon in the menu bar 

and via

Tools → Android → SDK Manager

Use a check mark to select the desired SDK platforms /Android versions and tools. Then click on

<Install .. packages..>.

Accept the licence with “Accept Licence” and press <Install> to start the download and installation process.

You should install the following packages as described above if they are not yet installed:

Tools required:

Android SDK tools

Android SDK platform tools

Android SDK build-tools (at least one, preferably the latest)

Android Version

At least one Android version must have been installed. After installation, the current Android version must appear in Android Studio, e.g.:

Android 4.4 API 19 or higher

At least the SDK platform package for an Android version is required. If you want to run the app on the emulator, you must install at least the ARM EABI v7a System Image. If you have an Intel-based computer, you should also install the Intel x86 Atom System Image since it is slightly faster.

Extras required:

Android Support Repository

Android Support Library

Google Play services

Google Repository

UBS driver (Windows) for Google telephones

First project 

This chapter describes the process to create an Android project in Android Studio. For this, you need to use a template provided in Android Studio. This template is called a "Blank template" and it creates a fully operational "Hello World" app.

You do not require any programming knowledge to create this project. You simply need to test the working environment. You need to check whether Android Studio can build an app and run it on an emulator or an Android telephone.

This is also a good opportunity to view the project structure of a project created in Android Studio and to get well-versed with the working environment.

Creating a new project

When open Android Studio for the first time after installing it, you will see a Welcome screen containing various selection options in the Quick Start menu.

From the Quick Start menu, select "New Project”; you will be guided through multiple screens to create the new project.

The "New Project” input screen will then be displayed.

Application name:

The name of the app that should later appear in the application, e.g. in the title bar or in Google Play Store.

You can select this name freely. The strings entered here are stored in the "app_name" variable in the "strings.xml" file and can be edited in this file if required. (Spaces are allowed)

Module name:

Rule - no spaces

Package name:

It is the “identification name" of the app. It is included in all Java files and in the AndroidManifest.xml file.

Project location:

The location where the current project is stored on the hard disk. It can be changed if required.

Minimum required SDK:

API 7, Android 2.1 is preset here by default. You can select a higher API/ Android version in many cases. For example, all Ads providers such as Google's Admob require the Android version API 8 as the minimum requirement.

The icon for the app

Here you can upload an image/clip art or enter text that should bear the app icon. If you select the clip art and text, you also need to select the foreground and background colours.

Click on <Next> to select the app template: When you select the Blank Activity template, a "Hello World" app will be created automatically.