Xamarin Mobile Development for Android Cookbook - Matthew Leibowitz - E-Book

Xamarin Mobile Development for Android Cookbook E-Book

Matthew Leibowitz

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Xamarin is used by developers to write native iOS, Android, and Windows apps with native user interfaces and share code across multiple platforms not just on mobile devices, but on Windows, Mac OS X, and Linux.
Developing apps with Xamarin.Android allows you to use and re-use your code and your skills on different platforms, making you more productive in any development. Although it’s not a write-once-run-anywhere framework, Xamarin provides native platform integration and optimizations. There is no middleware; Xamarin.Android talks directly to the system, taking your C# and F# code directly to the low levels.
This book will provide you with the necessary knowledge and skills to be part of the mobile development era using C#. Covering a wide range of recipes such as creating a simple application and using device features effectively, it will be your companion to the complete application development cycle.
Starting with installing the necessary tools, you will be guided on everything you need to develop an application ready to be deployed. You will learn the best practices for interacting with the device hardware, such as GPS, NFC, and Bluetooth. Furthermore, you will be able to manage multimedia resources such as photos and videos captured with the device camera, and so much more! By the end of this book, you will be able to create Android apps as a result of learning and implementing pro-level practices, techniques, and solutions. This book will ascertain a seamless and successful app building experience.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 603

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Xamarin Mobile Development for Android Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Working with Xamarin.Android
Introduction
Creating Xamarin.Android projects
Getting ready
How to do it...
How it works...
Creating user interface layouts
How to do it...
How it works...
There's more...
See also
Creating an options menu
How to do it...
How it works...
Supporting previous Android versions
How to do it...
How it works...
There's more...
Adding an action bar
How to do it...
How it works...
There's more...
See also
Navigating with the action bar
How to do it...
How it works...
There's more...
See also
Adding action bar action items
How to do it...
How it works...
Creating contextual action mode menu
How to do it...
How it works...
There's more...
See also
Creating contextual action mode menus
How to do it...
How it works...
There's more...
See also
Sharing code with other platforms
How to do it...
How it works...
2. Showing Views and Handling Fragments
Introduction
Using custom views with layouts
How to do it...
How it works...
There's more...
See also
Creating and using fragments
Getting ready
How to do it...
How it works...
See also
Preserving view and fragment state
How to do it...
How it works...
There's more...
See also
Navigating between fragments
How to do it...
How it works...
See also
Fragments and the action bar
How to do it...
How it works...
See also
Animating fragment navigation
How to do it...
How it works...
See also
Animating view and object properties
How to do it...
How it works...
There's more...
See also
Animating views on the UI
How to do it...
How it works...
There's more...
Adding a navigation drawer with fragments
How to do it...
How it works...
See also
Applying local styles and global themes
How to do it...
How it works...
3. Managing App Data
Introduction
Storing data with SharedPreferences
How to do it...
How it works...
There's more...
Using files and the filesystem
How to do it...
How it works...
There's more...
Reading bundled assets and resources
How to do it...
How it works...
See also
Parsing, processing, and generating XML
Getting ready
How to do it...
How it works...
There's more...
See also
Accessing data with ADO.NET
Getting ready...
How to do it...
How it works...
See also
Accessing data with SQLite.NET
How to do it...
How it works...
There's more...
See also
Encrypting databases with SQLCipher
How to do it...
How it works...
See also
Consuming content providers
Getting ready...
How to do it...
How it works...
There's more...
See also
Creating content providers
How to do it...
How it works...
See also
Backing up preferences and files to the cloud
How to do it...
How it works...
There's more...
See also
Backing up data to the cloud
Getting ready...
How to do it...
How it works...
There's more...
See also
4. Presenting App Data
Introduction
Implementing a ListView
How to do it...
How it works...
There's more...
See also
Using a SimpleAdapter
How to do it...
How it works...
There's more...
See also
Using custom ListView items
How to do it...
How it works...
There's more...
See also
Using a BaseAdapter with arbitrary data
How to do it...
How it works...
There's more...
See also
Using a CursorAdapter
How to do it...
How it works...
There's more...
See also
Optimizing the ListView
How to do it...
How it works...
There's more...
See also
Enabling fast scrolling
How to do it...
How it works...
There's more...
See also
Using section indexes
How to do it...
How it works...
There's more...
See also
Integrating app searchability
How to do it...
How it works...
There's more...
5. Communicating with the Outside World
Introduction
Consuming REST services with HttpClient
Getting ready
How to do it...
How it works...
See also
Obtaining a network state
How to do it...
How it works...
There's more...
See also
Handling network state changes
How to do it...
How it works...
There's more...
See also
Using DownloadManager
How to do it...
How it works...
See also
Accessing Bluetooth
How to do it...
How it works...
There's more...
See also
Transferring data via Bluetooth
How to do it...
How it works...
There's more...
See also
Receiving NFC events
Getting ready
How to do it…
How it works...
There's more...
See also
Writing NFC tags
How to do it...
How it works...
There's more...
See also
Transferring data via NFC
How to do it...
How it works...
Obtaining location coordinates and addresses
How to do it...
How it works...
There's more...
6. Using Background Tasks
Introduction
Asynchronous tasks
How to do it...
How it works...
There's more...
See also
Starting services
How to do it...
How it works...
There's more...
See also
Stopping services
How to do it...
How it works...
See also
Handling simultaneous service requests
How to do it...
How it works...
See also
Starting services automatically
How to do it...
How it works...
See also
Communicating with running services
How to do it...
How it works...
There's more...
See also
Critical services
How to do it...
How it works...
There's more...
7. Notifying Users
Introduction
Toasts
How to do it...
How it works...
There's more...
Alert dialogs
How to do it...
How it works...
There's more...
Alert fragments
Getting ready
How to do it...
How it works...
There's more...
See also
Embedded alert fragments
Getting ready
How to do it...
How it works...
There's more...
Selection alerts
How to do it...
How it works...
The notification builder
Getting ready
How to do it...
How it works...
There's more...
Ongoing notifications
Getting ready
How to do it...
How it works...
Custom notification views
Getting ready
How to do it...
How it works...
There's more...
Push notifications
Getting ready
How to do it...
How it works...
There's more...
8. Interacting with Other Apps
Introduction
Starting app components
How to do it...
How it works...
There's more...
Launching other apps
How to do it...
How it works...
There's more...
Obtaining data from activities
How to do it...
How it works...
Using BroadcastReceivers
How to do it...
How it works...
Scheduling tasks
How to do it...
How it works...
There's more...
Making phone calls
How to do it...
How it works...
There's more...
Intercepting phone calls
How to do it...
How it works...
There's more...
Sending SMS messages
How to do it...
How it works...
There's more...
Receiving SMS messages
How to do it...
How it works...
There's more...
9. Presenting Multimedia
Introduction
Playing audio
How to do it...
How it works...
There's more...
Playing audio in the background
How to do it...
How it works...
There's more...
Managing the audio volume
How to do it...
How it works...
There's more...
Recording an audio
How to do it...
How it works...
There's more...
Playing a video
How to do it...
How it works...
There's more...
Custom video controls
How to do it...
How it works...
Using the camera
How to do it...
How it works...
Creating a camera app
How to do it...
How it works...
There's more...
Handling high-resolution images
How to do it...
How it works...
Drawing on the canvas of a View
How to do it...
How it works...
There's more...
Drawing on the canvas of a SurfaceView
How to do it...
How it works...
There's more...
10. Responding to the User
Introduction
Responding to simple touches
How to do it...
How it works...
There's more...
Responding to scroll gestures
How to do it…
How it works...
There's more...
Responding to manipulation gestures
How to do it...
How it works...
Detecting rotate gestures
How to do it...
How it works...
Responding to custom user gestures
How to do it...
How it works...
There's more...
Listening to sensor data
How to do it...
How it works...
Listening for sensor triggers
How to do it...
How it works...
Discovering the environment
How to do it...
How it works...
Detecting device shakes
How to do it...
How it works...
There's more...
11. Connecting to Wearables
Introduction
Introducing wearable notifications
Getting ready
How to do it...
How it works...
Customizing wearable notifications
How to do it...
How it works...
There's more...
Creating wearable apps
Getting ready
How to do it...
How it works...
There's more...
Creating always-on wearable apps
How to do it...
How it works...
Creating dynamic always-on wearable apps
How to do it...
How it works...
Communicating with wearables
Getting ready
How to do it...
How it works...
There's more...
Building watch faces
Getting ready
How to do it...
How it works...
There's more...
Configuring watch faces
How to do it...
How it works...
There's more...
12. Adding In-App Billing
Introduction
Preparing for in-app billing
Getting ready
How to do it...
How it works...
Integrating in-app billing
Getting ready
How to do it...
How it works...
Listing available products
How to do it...
How it works...
There's more...
Purchasing products
How to do it...
How it works...
There's more...
Listing purchased products
How to do it...
How it works...
Consuming purchases
How to do it...
How it works...
13. Publishing Apps
Introduction
Protecting the content
Getting ready
How to do it...
How it works...
There's more...
Protecting the code
Getting ready
How to do it...
How it works...
Preparing the app package
How to do it...
How it works...
Shrinking the app package
How to do it...
How it works...
There's more...
Creating the app package
How to do it...
How it works...
There's more...
Uploading the app package
Getting ready
How to do it...
How it works...
Adding preview testers
How to do it...
How it works...
There's more...
Releasing for production
Getting ready
How to do it...
How it works...
Updating the app
Getting ready
How to do it...
How it works...
Index

Xamarin Mobile Development for Android Cookbook

Xamarin Mobile Development for Android Cookbook

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: November 2015

Production reference: 1191115

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78439-857-6

www.packtpub.com

Credits

Author

Matthew Leibowitz

Reviewers

Ole Falkerslev Kristensen

Frédéric Mauroy

Luca Zulian

Commissioning Editor

Amarabha Banerjee

Acquisition Editor

Reshma Raman

Content Development Editor

Dharmesh Parmar

Technical Editor

Abhishek R. Kotian

Copy Editor

Pranjali Chury

Project Coordinator

Paushali Desai

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Arvindkumar Gupta

Cover Work

Arvindkumar Gupta

About the Author

Matthew Leibowitz is a professional software engineer living in South Africa, and is currently working in a Xamarin development team. He has many years of professional experience as a programmer in developing systems ranging from servers, to desktops, to mobile devices.

He has a passion for programming and has recently been awarded Xamarin Certified Mobile Developer certification for his work. He has written various articles on his blog and has participated in several forums.

With his experience and great passion for development, Matthew spends his days continually looking for new ways to create a better experience for users and other developers. Some of his work is seen on GitHub under the username mattleibow. In addition to his public code, Matthew is active on Twitter with the same username.

I would like to thank my family for enduring my late nights and putting up with my strange behavior for the time it me took to put all this together. They have been very encouraging and have inspired me to share my knowledge with the world.

About the Reviewers

Ole Falkerslev Kristensen was a software developer at Nokia Mobile phone for more than 10 years, working with C, C++ and C#. He is now a professional Xamarin.Android developer in a startup company with 4-5 employees for the last 3 years.

He has also developed Android Apps for A.M.O Professional Loyalty programs and events apps. Also, he has also developed in Xamarin iOS.

Frédéric Mauroy discovered computers in the mid-80s and got his joy of programming with Basic. This new-found passion naturally led him to pursue an education in IT, where he learned C and C++. His first job allowed him to hone his skills in C, and he later sled towards C# with the amazing .Net framework. Having mainly worked in ASP.Net, he also developed Windows applications, and more recently, mobile applications for Android and iOS using PhoneGap and Xamarin.

He has made mobile applications for Viashopia and Alert112. You can get more details at the following links:

http://mauroy.eu

http://viashopia.com

http://alert112.com

Luca Zulian lives in Milan, Italy, and he is a skilled Microsoft .NET Framework developer, specializing in patterns and practices. In his many years as developer, he worked with numerous languages and technologies, and started with mobile development from a native approach to land on the Xamarin world. Now, he is experienced in large-scale enterprise applications along with a continuous phase of learning new languages and tools.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

With a rapid increase in the use of mobile devices everywhere, developing for Android takes advantage of this trend to reach the widest market available to any mobile platform. Along with creating awesome Android apps, Xamarin allows the use of the mature .NET Framework. .NET is a massive framework that is supported on almost all platforms, including iOS, Windows, Mac OS X, and Linux.

Developing apps with Xamarin.Android allows you to use and reuse your code and skills on different platforms, making you more productive in any sort of development. Although not a write-once-run-anywhere framework, Xamarin provides native platform integration and optimizations. There is no middleware; Xamarin.Android talks directly to the system, taking your C# and F# code directly to the low levels.

What this book covers

Chapter 1, Working with Xamarin.Android, allows us to create native Android apps with the strengths of C# and .NET. Using C#, we can develop native Android apps and at the same time have the ability to share code with other platforms.

Chapter 2, Showing Views and Handling Fragments, explores one of the most important parts of Android apps—the user interface, as it is the most visible part of an app. When creating apps for Android, there are numerous different ways available to create these interfaces.

Chapter 3, Managing App Data, consists of the program that almost all apps make use of to process data. Android provides many ways to manage data, each being different and useful for different purposes. Data can be stored in a file, a dictionary, or in a SQLite database.

Chapter 4, Presenting App Data, is only useful to a user if the user is able to view it. Android has several means to present data, the most common being some form of list or collection.

Chapter 5, Communicating with the Outside World, explains communication, which is possibly the most important part of interaction, both between humans and technology. Most Android devices are built with Cellular, Wi-Fi, Bluetooth, or NFC communication technologies.

Chapter 6, Using Background Tasks, is where the users expect mobile apps to be fluid, dynamic, and most of all, responsive. Long-running tasks, even those running for a few milliseconds, must be run in the background to keep an app responsive.

Chapter 7, Notifying Users, explains notifications, which draw the user's attention to let him/her know something has happened. Android apps can present notifications in several ways, ranging from a quick popup to a persistent message from a remote server.

Chapter 8, Interacting with Other Apps, explains that users have many apps installed on their Android devices. By developing apps to be aware of other apps, our apps can communicate and request data from these other apps.

Chapter 9, Presenting Multimedia, consists of an explanation of audio, video and photos, the most vivid means to convey information to the user. By making use of Android's many features, an app can present a user with dynamic images and sounds. This can be used to provide a function or enhance other functionality.

Chapter 10, Responding to the User, explicates that a user can interact with Android apps in many ways. The user can manipulate virtual objects using the device's touch screen, or trigger the sensors by moving the device in the physical world.

Chapter 11, Connecting to Wearables, is one of the newest things with regard to technology; it is the increase in wearable devices. Android Wear is a special version of Android that allows device manufacturers to create wearables and permit typical Android apps to run on them.

Chapter 12, Adding In-App Billing, elucidates Android app developers can capitalize on the fact that users are willing to pay for new or additional features in the app. This is especially the case with mobile games that support purchasing virtual products or subscriptions.

Chapter 13, Publishing Apps, posits that once an Android app has been created, the next step is to release it into the world. Google has created the Google Play store, which can be used to distribute mobile and wearable apps. Before distribution, mobile apps can be compressed and protected against malicious users.

What you need for this book

This book will provide you with the necessary knowledge and skills to be part of the mobile development era using C#. Covering a wide range of recipes such as creating a simple application and using device features effectively, it will be your companion during the complete application development cycle.

Starting from installing the necessary tools, you will be guided step-by-step on everything you need to develop an application, ready to be deployed. You will learn best practices for interacting with the device's hardware, such as the GPS, NFC, and Bluetooth. Furthermore, you will be able to manage multimedia resources, such as photos and videos captured with the device's camera, and much more!

Who this book is for

If you are a C#/.NET developer with no previous experience in Android development, or you are a Java developer who wants to create complete Android applications in C# and deploy them to the Play Store, then this book is ideal for you. Having no experience with Xamarin will not hamper your interests.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Working with Xamarin.Android

In this chapter, we will cover the following recipes:

Creating Xamarin.Android projectsCreating user interface layoutsCreating an option menuSupporting previous Android versionsAdding an action barNavigating with the action barAdding action bar action itemsCreating contextual action mode menuSharing code with other platforms

Introduction

Xamarin.Android allows us to create native Android applications using the same UI controls we would use in Java, with the flexibility of C#, the power of the .NET Base Class Library, and two first-class IDEs.

Android development with Xamarin can be done on either Mac OS X or Windows, using either Visual Studio or Xamarin Studio. This variety provides us with our choice of how we want to work to create awesome apps.

This book will enable us, as developers, to create amazing, professional apps for the Android ecosystem. This knowledge can be used on so many platforms, from TVs and smartphones to watches, wearables, and many other Android-powered devices.

This chapter covers some of the most common tasks and steps to getting our app ready for development. We will learn how to create a new app, add support for the old versions of Android, and get started with the user interface used in all Android apps. We will also look at just how powerful the Xamarin.Android platform is by looking how we can share code with many other platforms, including Windows Phone, iOS, Windows, and Mac.

Adding action bar action items

The fundamental purpose of an action bar, besides navigation, is to present the user with a set of actions that can be performed.

How to do it...

By simply using the action bar, all the action items are added to the overflow:

The XML for ActionBar items is exactly the same as the options menu:
<menu ... > <item android:id="@+id/action_refresh" android:icon="@drawable/ic_action_refresh" android:title="@string/action_refresh"/> </menu>

However, we can customize what items are displayed, and how they are displayed:

To add action items with images to the actual ActionBar property, as well as more complex items, all that is needed is an attribute in the XML, showAsAction:
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom"/> </menu>
If we wish to add custom views, such as a search box, to the action bar, we make use of the actionViewClass attribute:
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionViewClass="android.support.v7.widget.SearchView"/> </menu>
If the view is in a layout resource file, we use the actionLayout attribute:
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionLayout="@layout/action_rating"/> </menu>
Sometimes, we may wish to only display the icon initially and then, when the user taps the icon, expand the item to display the action view:
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom|collapseActionView"/> </menu>

How it works...

Action item buttons are just traditional options menu items but are optionally always visible on the action bar.

The underlying logic to handle item selections is the same as that for the traditional options menu. No change is required to existing code inside the OnOptionsItemSelected() method.

The value of the showAsAction attribute can be ifRoom, never, or always. This value can optionally be combined, using a pipe, with withText and/or collapseActionView.

Sharing code with other platforms

One of the major reasons for using Xamarin.Android is the use of C# as the programming language. But this is not the only benefit as that same C# code can be shared with other platforms, such as iOS, Windows, or Mac OS.

How to do it...

First, we are going to create the project structure that we are going to use to create our cross-platform app. In this example, we will only target Xamarin.Android and the Console, but extra platforms can be added.

Open Xamarin Studio or Visual Studio and create an empty solution. For example, we are going to call this one XamarinCodeSharing.In this solution, create three projects:
An Android project named XamarinCodeSharing.DroidA console application named XamarinCodeSharing.DesktopA portable class library named XamarinCodeSharing.Core
Open the project settings for XamarinCodeSharing.Core. If you are using Xamarin Studio, navigate to Build | General. Or if you are using Visual Studio, navigate to Library and then click on Change under the Targeting section.Note the various platform options available, including iOS, Android, Windows, and several others, some with version options. Ensure that the .NET4.5 and the Xamarin.Android boxes are selected as these are the platforms we are going to need.To make things easier, we are going to use a NuGet package, Microsoft.Net.Http, which simplifies the process of using HTTP and REST services. Install this package into each of the three projects.Add a project reference from XamarinCodeSharing.Droid to XamarinCodeSharing.Core and a project reference from XamarinCodeSharing.Desktop to XamarinCodeSharing.Core.