39,59 €
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:
Seitenzahl: 603
Veröffentlichungsjahr: 2015
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
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
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.
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.
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.
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.
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.
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.
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!
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.
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.
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.
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.
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 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.
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.
In this chapter, we will cover the following recipes:
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.
The fundamental purpose of an action bar, besides navigation, is to present the user with a set of actions that can be performed.
By simply using the action bar, all the action items are added to the overflow:
However, we can customize what items are displayed, and how they are displayed:
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.
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.
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.
