Android Programming Cookbook I
Total Page:16
File Type:pdf, Size:1020Kb
Android Programming Cookbook i Android Programming Cookbook Android Programming Cookbook ii Contents 1 Android Tutorial For Beginners 1 1.1 What is Android?...................................................1 1.2 Installing Android Studio...............................................1 1.3 Android versions and Android SDK Manager....................................7 1.4 Supporting different screen sizes...........................................7 1.5 Android Project Structure...............................................8 1.6 Create "Hello Android World" application......................................9 1.6.1 Create a New Android Studio Project....................................9 1.6.2 Create the source code of a simple FirstAndroidApplication Activity.................... 13 1.6.3 Create the layout of the project........................................ 14 1.6.4 Android Manifest............................................... 14 1.6.5 Edit the FirstAndroidApplication dimensions................................ 15 1.6.6 Edit the FirstAndroidApplication strings................................... 15 1.6.7 Add the drawable for every screen density.................................. 15 1.6.8 Build, compile and run............................................ 16 1.7 Download the Android Studio Project........................................ 18 1.8 How to continue?................................................... 18 2 Android Project migration from Eclipse to Android Studio 19 2.1 Why to use Android Studio over Eclipse ADT?................................... 19 2.2 Android Studio new project structure......................................... 20 2.3 Gradle and build.gradle................................................ 20 2.4 Simple Eclipse ADT project migration to Android Studio.............................. 21 2.5 Java code and resources migration.......................................... 27 2.6 AndroidManifest.xml and build.gradle file...................................... 29 2.7 Download the Android Studio Project........................................ 32 Android Programming Cookbook iii 3 Android Google Maps v2 Tutorial 33 3.1 Create a Google Maps API key............................................ 33 3.2 Create a New Android Application Project...................................... 39 3.3 Importing Google Play Services in your project................................... 46 3.4 Create the layout of the main Google Maps v2.................................... 51 3.5 Create the source code of the main AndroidGoogleMapsActivity.......................... 52 3.6 Creating the source code of the helper class CustomMarker.java.......................... 58 3.7 Creating the source code of the helper class LatLngInterpolator.java........................ 59 3.8 Creating the source code of the helper class MarkerAnimation.java......................... 61 3.9 Modifying the AndroidManifest.xml......................................... 63 3.10 Build, compile and run................................................ 64 3.11 Download the Eclipse Project............................................. 66 4 Android Start Service on Boot Example 67 4.1 Create a New Android Studio Project......................................... 67 4.2 Create the layout and the source code of a simple AndroidStartServiceOnBoot Activity.............. 71 4.3 Creating the source code of the BroadcastReceiverOnBootComplete Service.................... 72 4.4 Creating the source code of the AndroidServiceStartOnBoot Service........................ 72 4.5 Editing the Android Manifest xml.......................................... 73 4.6 Build, compile and run................................................ 74 4.7 Download the Android Studio Project........................................ 79 5 Android Bluetooth Connection Example 80 5.1 Introduction...................................................... 80 5.2 Create a New Android Studio Project......................................... 80 5.3 Create the layout of the BluetoothChat........................................ 84 5.4 Create the source code of the BluetoothChat..................................... 85 5.5 Create the source code of the BluetoothChatService................................. 90 5.6 Create the layout of the DeviceListActivity..................................... 96 5.7 Create the source code of the DeviceListActivity.................................. 97 5.8 AndroidManifest.xml................................................. 100 5.9 build.gradle...................................................... 101 5.10 Build, compile and run................................................ 101 5.11 Download the Android Studio Project........................................ 104 Android Programming Cookbook iv 6 Android Multitouch Example 105 6.1 Create a New Android Studio Project......................................... 105 6.2 Create the layout of the project............................................ 109 6.3 Creating the source code of the TouchableFrameLayout FrameLayout....................... 110 6.4 Creating the source code of the main AndroidMultitouchActivity Activity..................... 113 6.5 Create the strings.xml................................................. 115 6.6 Android Manifest................................................... 115 6.7 build.gradle...................................................... 115 6.8 Build, compile and run................................................ 116 6.9 Download the Android Studio Project........................................ 118 7 Android StackView Example 119 7.1 Create a New Android Studio Project......................................... 119 7.2 Create the layout of the AndroidStackViewActivity................................. 123 7.3 Create the layout of the StackView items....................................... 124 7.4 Create the source code of the StackItems....................................... 124 7.5 Create the source code of the StackAdapter..................................... 124 7.6 Create the source code of the AndroidStackViewActivity.............................. 125 7.7 AndroidManifest.xml................................................. 126 7.8 build.gradle...................................................... 127 7.9 Build, compile and run................................................ 127 7.10 Download the Android Studio Project........................................ 129 8 Android ViewPager Example 130 8.1 Create a New Android Studio Project......................................... 130 8.2 Create the layout of the main AndroidViewPagerExample.............................. 134 8.3 Create the source code of the main AndroidViewPagerExample Activity...................... 135 8.4 Create the layout of the main FragmentViewPager.................................. 136 8.5 Create the source code of the main FragmentViewPager support.v4.app.Fragment................. 136 8.6 Android Manifest................................................... 137 8.7 Composing build.gradle file.............................................. 138 8.8 Build, compile and run................................................ 138 8.9 Download the Android Studio Project........................................ 143 Android Programming Cookbook v Copyright(c) Exelixis MediaP.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced intoa retrieval system, or transmitted, in any form or by any means(electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of the copyright owner. Android Programming Cookbook vi Preface Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. Android’s user interface is mainly based on direct manipulation, using touch gestures that loosely correspond to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen objects, along with a virtual keyboard for text input. In addition to touchscreen devices, Google has further developed Android TV for televisions, Android Auto for cars, and Android Wear for wrist watches, each with a specialized user interface. Variants of Android are also used on notebooks, game consoles, digital cameras, and other electronics. Android has the largest installed base of all operating systems of any kind. Android has been the best selling OS on tablets since 2013, and on smartphones it is dominant by any metric. (Source: https://en.wikipedia.org/wiki/Android_(operating_system)) In this ebook, we provide a compilation of Android programming examples that will help you kick-start your own web projects. We cover a wide range of topics, from Services and Views, to Google Maps and Bluetooth functionality. With our straightforward tutorials, you will be able to get your own projects up and running in minimum time. Android Programming Cookbook vii About the Author Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer. Android Programming Cookbook 1 / 143 Chapter 1 Android Tutorial For Beginners There