Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 151 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
151
Dung lượng
8,22 MB
Nội dung
Android Programming Cookbook Android Programming Cookbook i Android Programming Cookbook ii Contents Android Tutorial For Beginners 1.1 What is Android? 1.2 Installing Android Studio 1.3 Android versions and Android SDK Manager 1.4 Supporting different screen sizes 1.5 Android Project Structure 1.6 Create "Hello Android World" application 1.6.1 Create a New Android Studio Project 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 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 Android Google Maps v2 Tutorial iii 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 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 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 Android Multitouch Example iv 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 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 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 Copyright (c) Exelixis Media P.C., 2016 All rights reserved Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced into a 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 v 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 / 143 Chapter Android Tutorial For Beginners There are lots of reasons why more and more people are interested in learing how to be able to develop Android applications Unarguably, Android is the most popular mobile operating system, with almost billion devices activated and it offers a unified approach to application development for mobile devices That means, that developers need only develop for Android, and their applications will be able to run on different devices powered by Android This particular asset gives Android endless possibilites! This means that and application that is designed to work on mobile phone devices can be also transfered to Android powered TV sets or Android Car systems This is why, Android is an exciting space to make apps that can help you in every aspect of your life, can help you communicate, organize, educate, entertain or just to make your life easier in every device that they might run on! In this special example, we are going to set our Android Development Studio IDE, make our very first Android application and discover the Android Development world in the easiest possible way The mobile development world can be very fun, because the direct results we see when creating our own application, can be highly motivating and rewarding 1.1 What is Android? Android is a mobile operating system currently developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets And as we said before, Android offers a unified approach to application development for mobile devices Android is an open-source operating system named Android Google has made the code for all the low-level "stuff" as well as the needed middleware to power and use an electronic device, and gave Android freely to anyone who wants to write code and build the operating system from it There is even a full application framework included, so third-party apps can be built and installed, then made available for the user to run as they like The "proper" name for this is the Android Open Source Project, and this is what people mean when they say things like Android is open and free Android, in this iteration, is free for anyone to use as they like 1.2 Installing Android Studio In order to write an Android application, we are going to need a development environment Google has made a very useful tool for all Android Developers, the Android Studio Android Studio is the official IDE for Android development, and with a single download includes everything you need to begin developing Android apps Included in the download kit, are the Software Development Kit (SDK), with all the Android libraries we may need, and the infrastructure to download the many Android emulator instances, so that we can initially run our application, without needing a real device So, we are going to download and install Android Studio Android Programming Cookbook / 143 First we have to have installed the Java Development Kit (JDK) from Oracle If you not, please you should download the latest JDK from the Oracle’s special section here Figure 1.1: Android Studio Installation - step Android Programming Cookbook 7.10 Download the Android Studio Project This was an example of Android StackView Download You can download the full source code of this example here: AndroidStackViewExample 129 / 143 Android Programming Cookbook 130 / 143 Chapter Android ViewPager Example One of the most popular views in Android is the ViewPager, a view that helps us slide from one "screen" to another like a "slideshow" Android ViewPager view can be found in many cases, in tutorial or introductory screens, on tab views and galleries We are going to create an AppCompatActivity, and we are going to add a ViewPager reference and in it, as well as a FragmentPagerAdapter that will help us navigate between the Fragments that our ViewPager will consist of and that we are going to create Then, we are going to make instances of Fragments that will be added in our Adapter The Android ViewPager has default swipe-gestures from one "screen" to another, and we not need to create any gestures in order to transit between the pages So, in this example, we are going to show, how we can implement an Android ViewPager Let’s start For our example will use the following tools in a Windows 64-bit or an OS X platform: • JDK 1.7 • Android Studio 1.3.2 • Android SDK 6.0 Let’s take a closer look: 8.1 Create a New Android Studio Project Open Android Studio and choose Start a new Android Studio Project in the welcome screen Android Programming Cookbook Figure 8.1: Welcome to Android Studio screen Choose Start a new Android Studio Project Specify the name of the application, the project and the package 131 / 143 Android Programming Cookbook Figure 8.2: Configure your new project screen Add your application name and the projects package name In the next window, select the form factors your app will run on 132 / 143 Android Programming Cookbook 133 / 143 Figure 8.3: Target Android Devices screen In the next window you should choose Add no activity In this example, we are going to create our Activity Android Programming Cookbook 134 / 143 Figure 8.4: Add an activity to Mobile Choose: Add no activity Now, our project has just been created! 8.2 Create the layout of the main AndroidViewPagerExample The AndroidViewPagerExample is the main Activity of our example, and this is the Activity in which we are going to create a ViewPager instance and set its FragmentPagerAdapter Now, we are going to make the layout of this Activity, which will be a simple layout xml for the AndroidViewPagerExample.class, that consists of a LinearLayout with vertical orientation, that includes a ViewPager Add a new xml file inside /res/layout folder, with name activity_main.xml We should have the /res/layout/activi ty_main.xml file and paste the code below activity_main.xml 8.3 Create the source code of the main AndroidViewPagerExample Activity Add a new Java class inside src/com.javacodegeeks.androidViewPagerExample/ so that we are going to have the src/com.javacodegeeks.androidViewPagerExample/AndroidViewPagerExample.java file and paste the code below AndroidViewPagerExample.java package com.javacodegeeks.examples.androidViewPagerExample; import import import import import android.support.v4.app.FragmentManager; android.support.v4.app.FragmentPagerAdapter; android.os.Bundle; android.support.v4.view.ViewPager; android.support.v7.app.AppCompatActivity; public class AndroidViewPagerExample extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); } private class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public android.support.v4.app.Fragment getItem(int pos) { switch (pos) { case 0: return FragmentViewPager.newInstance(getString(R.string.title_section1) , R.drawable.rock); case 1: return FragmentViewPager.newInstance(getString(R.string.title_section2) , R.drawable.paper); case 2: return FragmentViewPager.newInstance(getString(R.string.title_section3) , R.drawable.scissors); default: return FragmentViewPager.newInstance(getString(R.string.title_section1) , R.drawable.rock); } } @Override public int getCount() { ← ← ← ← Android Programming Cookbook 136 / 143 return 3; } } } 8.4 Create the layout of the main FragmentViewPager The FragmentViewPager is the Fragment of our example, from which we are going to recreate instances and add them in the ViewPager Now, we are going to make the layout of this Fragment, which will be a simple layout xml for the FragmentViewPager.class, that consists of a LinearLayout with vertical orientation, that includes a TextView and an ImageView Add a new xml file inside /res/layout folder, with name fragment_main.xml We should have the /res/layout/frag ment_main.xml file and paste the code below fragment_main.xml 8.5 Create the source code of the main FragmentViewPager support.v4.app.Fragment Add a new Java class inside src/com.javacodegeeks.androidViewPagerExample/ so that we are going to have the src/com.javacodegeeks.androidViewPagerExample/FragmentViewPager.java file and paste the code below FragmentViewPager.java package com.javacodegeeks.examples.androidViewPagerExample; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; Android Programming Cookbook 137 / 143 import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class FragmentViewPager extends android.support.v4.app.Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); TextView tv = (TextView) v.findViewById(R.id.title); tv.setText(getArguments().getString("text")); ImageView imageView = (ImageView) v.findViewById(R.id.image); imageView.setBackgroundResource(getArguments().getInt("img")); return v; } public static FragmentViewPager newInstance(String text, int image) { FragmentViewPager f = new FragmentViewPager(); Bundle b = new Bundle(); b.putString("text", text); b.putInt("img", image); f.setArguments(b); return f; } } 8.6 Android Manifest The AndroidManifest.xml of our project is simple and contains the main Activity of our example: AndroidManifest.xml 8.7 Composing build.gradle file We should add the AppCompat V7 support support library in our project We can this as a dependency to our application via build.gradle file build.gradle apply plugin: ’com.android.application’ android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.javacodegeeks.examples.androidViewPagerExample" minSdkVersion 14 targetSdkVersion 23 versionCode versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(’proguard-android.txt’), ’proguard-rules ← pro’ } } } dependencies { compile fileTree(dir: ’libs’, include: [’*.jar’]) compile ’com.android.support:appcompat-v7:23.1.1’ } 8.8 Build, compile and run When we build, compile and run our project, the main AndroidViewPagerExample should look like this: Android Programming Cookbook 139 / 143 Figure 8.5: This is the first Fragment of our ViewPager Android Programming Cookbook 140 / 143 Figure 8.6: This is the second Fragment of our ViewPager Android Programming Cookbook 141 / 143 Figure 8.7: This is the third Fragment of our ViewPager Android Programming Cookbook 142 / 143 Figure 8.8: This is the transition between two Fragments Android Programming Cookbook 8.9 Download the Android Studio Project This was an example of Android ViewPager Example Download You can download the full source code of this example here: AndroidViewPagerExample 143 / 143 .. .Android Programming Cookbook Android Programming Cookbook i Android Programming Cookbook ii Contents Android Tutorial For Beginners 1.1 What is Android? ... step Android Programming Cookbook / 143 Figure 1.4: Android Studio Installation - step Android Programming Cookbook / 143 Figure 1.5: Android Studio Installation - step Android Programming Cookbook. .. here Figure 1.1: Android Studio Installation - step Android Programming Cookbook / 143 Figure 1.2: Android Studio Installation - step Android Programming Cookbook / 143 Figure 1.3: Android Studio