Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Xamarin Mobile Application Development for Android - Second Edition
Xamarin Mobile Application Development for Android - Second Edition
Xamarin Mobile Application Development for Android - Second Edition
Ebook610 pages4 hours

Xamarin Mobile Application Development for Android - Second Edition

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Develop, test, and deliver fully-featured Android applications using Xamarin

About This Book
  • Build and test multi-view Android applications using Xamarin.Android
  • Work with device capabilities such as location sensors and the camera
  • A progressive, hands-on guide to develop stunning Android applications using Xamarin
Who This Book Is For

If you are a C# developer who wants to develop Android apps and enhance your existing skill set, then this book is ideal for you. Good working knowledge of C#, .NET, and object-oriented software development is assumed.

What You Will Learn
  • Build a multi-view, orientation-aware Android application with navigation
  • Lay out content using the LinearLayout, RelativeLayout, and TableLayout layout managers
  • Use a ListView (AdapterView) and Adapter to build a view that is populated from server data
  • Consume REST web service to perform GET, UPDATE, DELETE operation
  • Use Android SQLite for data persistence and caching
  • Capture the current location of a device, determine the street address, and integrate with the map app
  • Test, debug, and deploy an Android app
In Detail

Technology trends come and go, but few have generated the excitement, momentum, or long-term impact that mobile computing has. Mobile computing impacts people's lives at work and at home on a daily basis. Many companies and individual developers are looking to become a part of the movement but are unsure how to best utilize their existing skills and assets. The Xamarin suite of products provides new opportunities to those who already have a significant investment in C# development skills and .NET code bases, and would like to enter into this new, exciting world.

This example-oriented guide provides a practical approach to quickly learn the fundamentals of Android app development using C# and Xamarin.Android. It will lead you through building an Android app step-by-step with steadily increasing complexity.

Beginning with an overview of the Android and Xamarin platforms to provide you with a solid understanding of the underlying platform, we gradually walk through building and testing a Points of Interest Android app using C# and the Xamarin.Android product. You will learn to create ListView and add detail view to your Android application. You will handle application behaviors on orientation changes, before learning the different techniques to manage resources and layouts to support multiple screen sizes. You will then access a SQLite database in a cross-platform way and add location features to your application. Finally, you will add camera integration to your application and deploy your app to the various Android app stores.

Style and approach

An example-oriented, comprehensive guide to gain an understanding of both the Android and Xamarin platforms.

LanguageEnglish
Release dateAug 24, 2015
ISBN9781785280634
Xamarin Mobile Application Development for Android - Second Edition
Author

Nilanchala Panigrahy

Nilanchala Panigrahy is a software developer, community speaker, and thought leader in the fusion of design and mobile technologies. Currently, he is employed as a lead application developer by Ness Technologies (http://www.ness-ses.com), a leading global technology services provider. He has over 8 years of experience in developing rich mobile applications in Java, C#, Android, Xamarin, HTML5, and PhoneGap. Over the years, he has published 20 plus Android applications in the consumer and enterprise spaces and authored over 200 articles/tutorials on Android and Xamarin application development, which are freely available on the Web. You can find more information about him on his blog at http://javatechig.com or contact him via his Twitter handle at, @npanigrahy.

Related to Xamarin Mobile Application Development for Android - Second Edition

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Xamarin Mobile Application Development for Android - Second Edition

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Xamarin Mobile Application Development for Android - Second Edition - Nilanchala Panigrahy

    Table of Contents

    Xamarin Mobile Application Development for Android Second Edition

    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

    Downloading the color images of this book

    Errata

    Piracy

    Questions

    1. The Anatomy of an Android App

    The Android platform

    Versions of Android

    The Linux kernel

    Native libraries

    The Android runtime

    The application framework

    The application layer

    The building blocks of the Android application

    The Android packages (.apk)

    The application manifest

    Activities

    The life cycle of an activity

    The events of an activity

    Fragments

    The fragment life cycle

    Services

    Content providers

    Broadcast receivers

    Views and ViewGroups

    Declarative versus programmatic View creation

    User interface widgets

    Common layouts

    Adapter layouts

    XML layout files

    Element and attribute names

    The View and layout identifiers

    Using XML layouts from activities

    Intents

    Resources

    The R.java file

    Summary

    2. The Xamarin.Android Architecture

    Getting started with Xamarin

    Why Xamarin.Android?

    The benefits of using Xamarin.Android

    The drawbacks of using Xamarin.Android

    What is Mono?

    Mono and Dalvik side by side

    The Java Native Interface

    Peer objects

    Xamarin.Android application packaging

    The Android bindings design

    Design principles

    C# properties

    Delegates

    Constants to enumerations

    Development environments

    Xamarin Studio

    Xamarin for Visual Studio

    IDE comparison

    Compatibility

    Summary

    3. Creating the Points Of Interest App

    The sample POIApp

    Installing Xamarin.Android

    Installing platforms and tools

    Tools

    The Android platform APIs

    The Android platform Extras

    Creating Android Virtual Devices

    Cloning a virtual device

    Speeding up the Android emulator

    Using the x86 emulator

    Third-party emulator solutions

    The Xamarin Android Player

    Genymotion

    Creating the POI app

    The Xamarin Studio IDE

    The Project Options view

    Understanding the project structure

    The Xamarin Studio layout designer

    The designer content layout

    The Document Outline and Properties pads

    Toolbox

    Setting the target framework

    Setting the app icon and package name

    The launcher activity

    Running and debugging the app

    Debugging with an Android device

    Enabling USB debugging

    Installing a USB driver

    Running apps on a device

    Behind the scenes

    The peer object

    The AndroidManifest.xml file

    Summary

    4. Adding a List View

    Creating the POI ListView layout

    Creating POIListActivity

    Creating the POI list row layout

    Adding a RelativeLayout view group

    Adding an ImageView widget

    Adding a LinearLayout widget

    Adding the name and address TextView classes

    Adding the distance TextView

    Creating the PointOfInterest apps entity class

    Populating the ListView item

    Creating POIListViewAdapter

    Implementing a constructor

    Implementing Count { get }

    Implementing GetItemId()

    Implementing the index getter method

    Implementing GetView()

    Reusing row Views

    Populating row Views

    Populating the list thumbnail image

    Using UrlImageViewHelper

    Adding Internet permissions

    Hooking up POIListViewAdapter

    Consuming the web service

    An introduction to web services

    Deploying the POI web service

    Consuming REST web services asynchronously

    Creating the POIService class

    Asynchronous programming with async and await

    Serializing and deserializing using Json.NET

    Updating POIListActivity

    Adding actions to ActionBar

    Defining the menu XML file

    Setting menus in OnCreateOptionsMenu()

    Handling selection in OnOptionsItemSelected()

    Handling the ListView click event

    Handling no network condition

    Toast

    Summary

    5. Adding a Details View

    Creating the POIDetail layout

    Understanding ScrollView

    Using the TableLayout manager

    Working with EditText's InputType

    Creating POIDetailActivity

    Adding navigation to POIDetailActivity

    Navigation on the POIList item click

    Receiving the data in POIDetailActivity

    Binding variables to controls

    Populating user interface widgets

    Adding the save and delete actions

    Disabling the delete action

    Adding the save and delete actions in POIService

    Consuming the web service to create or update a POI

    Adding the CreateOrUpdatePOIAsync method to POIService

    Consuming the web service to delete the POI

    Adding the DeletePOIAsync method to POIService

    Creating SavePOI()

    Creating DeletePOI()

    Adding validation

    Using the EditText.Error property

    Adding a delete confirmation prompt

    Refreshing POIListActivity

    Summary

    6. Making Your App Orientation-aware

    The Android behavior on configuration changes

    Locking the Android application orientation

    Dynamically requesting the orientation

    Saving the activity state for configuration changes

    Manually saving the activity state

    Retaining the POI list scroll position

    Building the orientation-aware layout

    Manually handling the orientation behavior

    Summary

    7. Designing for Multiple Screen Sizes

    An introduction to Android tablets

    Creating an Android tablet emulator

    An introduction to fragments

    Creating a new fragment

    Making POIApp compatible with Android tablets

    Using fragments for displaying POIDetails

    Creating the POIDetailsFragment layout

    Creating the POIDetailFragment

    Working with the save and delete actions

    Adding the POIDetailFragment to POIDetailActivity

    Modifying the POI details activity layout

    Refactoring POIDetailActivity to add POIDetailFragment

    Using the list fragment for displaying the POI list

    Understanding ListFragment

    Creating the POIListFragment layout

    Creating the fragment to display the POI list using POIListFragment

    Adding POIListFragment to POIListActivity

    Modifying the POI list activity layout

    Adding POIListFragment to POIListActivity

    Creating a multi-pane layout for tablets

    Updating the POIApp to support the multi-pane split layout

    Working with DialogFragment

    Fragments for older Android devices

    Handling configuration changes with fragments

    Summary

    8. Creating Data Storage Mechanisms

    Data storage in Android

    Shared preferences

    The SQLite database storage

    Using the SQLite.net ORM component

    Using attributes to mark POIApp to be persisted

    Adding a database helper class

    Creating or updating the POI record

    Reading POI details from the database

    Deleting the POI data from the database

    Using the Xamarin.Android NUnitLite

    Setting up for tests

    Creating the test methods

    The CreatePOI test

    The DeletePOI test

    Executing the tests

    Implementing the caching logic to POIApp

    Summary

    9. Making POIApp Location Aware

    Working with location services

    Setting the app permissions

    Configuring the emulator

    Obtaining an instance of LocationManager

    Requesting location change notifications

    Implementing ILocationListener

    Adding location services to POIApp

    Adding location services to the POI list

    Adding location services to POI details

    Updating the user interface

    Adding the code

    Mocking location data for testing

    Getting an address for a location

    Keeping the user informed

    Adding map integration

    Navigating to the Map app

    Checking for registered map apps

    Summary

    10. Adding the Camera App Integration

    Picking an integration approach

    Permissions and features

    Configuring the emulator

    Extending the data service

    Implementing GetFileName()

    Implementing GetImage()

    Implementing DeleteImage()

    Capturing an image from POIDetailFragment

    Adding UI elements

    Creating the camera Intent

    Checking for registered camera apps

    Providing additional information with the Intent

    Providing a filename and location

    Providing a size limit

    Starting the Intent

    Completing the NewPhotoClicked() method

    Processing the results of the Intent

    Upload an image using an HTTP multipart

    Summary

    11. Publishing an App to the App Store

    Preparing the application for release

    Disabling the debug mode

    Configuring linking options

    Side effects of linking

    Selecting supported ABIs

    Verifying the package name, icon, and app version

    The package name

    The application name and icon

    The application versioning scheme

    Review user permissions

    Publishing a signed APK for uploading

    Android keystores

    Publishing from Xamarin.Android

    Republishing the app

    Different app distribution options

    Self-publishing via website links or e-mails

    Publishing on Google Play

    Third-party app stores

    Summary

    Index

    Xamarin Mobile Application Development for Android Second Edition


    Xamarin Mobile Application Development for Android Second Edition

    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: January 2014

    Second edition: August 2015

    Production reference: 1200815

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-78528-037-5

    www.packtpub.com

    Credits

    Author

    Nilanchala Panigrahy

    Reviewers

    Mart Roosmaa

    Ed Snider

    Adrian Stevens

    Michael Williams

    Commissioning Editor

    Kunal Parikh

    Acquisition Editor

    Meeta Rajani

    Content Development Editor

    Arun Nadar

    Technical Editor

    Madhunikita Sunil Chindarkar

    Copy Editor

    Rashmi Sawant

    Project Coordinator

    Neha Bhatnagar

    Proofreader

    Safis Editing

    Indexer

    Hemangini Bari

    Production Coordinator

    Nitesh Thakur

    Cover Work

    Nitesh Thakur

    About the Author

    Nilanchala Panigrahy is a software developer, community speaker, and thought leader in the fusion of design and mobile technologies. Currently, he is employed as a lead application developer by Ness Technologies (http://www.ness-ses.com), a leading global technology services provider. He has over 8 years of experience in developing rich mobile applications in Java, C#, Android, Xamarin, HTML5, and PhoneGap.

    Over the years, he has published 20 plus Android applications in the consumer and enterprise spaces and authored over 200 articles/tutorials on Android and Xamarin application development, which are freely available on the Web.

    You can find more information about him on his blog at http://javatechig.com or contact him via his Twitter handle at, @npanigrahy.

    Firstly, I'd like to thank Bibhu, whose love, patience, and understanding pushes me to a new high every time I take up new opportunities. Your support makes everything I do possible. A big thanks to my family for their love, encouragement, and belief.

    Thanks to the entire editorial team at Packt Publishing, especially Arun and Meeta, who had to contend with many delays while I juggled between full-time application development and my book writing commitments.

    A special thanks to all the technical reviewers of this book, who did an excellent job to ensure that the content of this book is up to date and the source code is error-free.

    I would like to thank you all for choosing this book. I hope you will find all the information you seek.

    About the Reviewers

    Mart Roosmaa is a senior software consultant. He has worked with a wide range of technologies over the years, ranging from backend to embedded to desktop development, and most recently, mobile app development. He prefers to create Android applications with Xamarin/C#, as it allows him to deliver better results faster.

    Ed Snider is a senior software developer at InfernoRed Technology, a speaker, and a Xamarin MVP based in the DC/Northern VA area. He has a passion for mobile development. He regularly speaks at local user groups and community events and is the organizer of the DC and Northern VA Mobile .NET Developers group. He is primarily focused on building mobile solutions on the Windows, iOS, and Android platforms for small and large organizations and has been working with .NET for over 10 years. He blogs at http://www.edsnider.net and can be found on Twitter at http://www.twitter.com/edsnider.

    Adrian Stevens has over 14 years of experience in developing mobile applications, specializing in C# and C++ cross-platform development. He founded and led a successful mobile consulting company.

    Adrian is also an experienced author and instructor, having written several published articles on mobile and IoT development as well as authored the curriculum on cross-platform development.

    Adrian's expertise includes wireless communication, architecture, data processing, and mathematics.

    He is based out of Vancouver, Canada, and co-runs a C# cross-platform development Meetup community.

    Michael Williams is a hybrid mobile architect. He is an insightful, result-driven IT professional with notable experience in creating his own business, developing mobile applications for IOS, Android, and Windows Phone. He also has experience in game development, directing a broad range of corporate IT initiatives, and participating in the planning, analysis, and implementation of solutions in support of business objectives. He has hands-on experience in all the stages of system development, including the requirements definition, design, architecture, testing, and support.

    His previous employers include IBM, Sound Therapy International, and Bepoz. He currently works for DNS Technology, Radio DI, and Flush Arcade (his own company).

    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 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 Packt

    Copy and paste, print, and bookmark content

    On 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

    Xamarin is built on top of Mono, an open source version of the .NET framework, based on the ECMA standards. Xamarin brings you a set of tools that includes its own C# compiler and a Common Language Runtime (CLR). The Mono framework source project is maintained by Xamarin, a San Francisco-based company (formerly by Novell and originally by Ximian). The prime intention of the Mono project was to make the .NET platform compatible with other non-Windows platforms such as Linux.

    After Attachmate acquired Novell in April 2011, the future of the Mono platform was pushed into dark. A few months later, Miguel de Icaza, a former Novell employee, founded a company called Xamarin and declared to continue using the Mono platform for commercial software development. Since then, Xamarin has sponsored the Mono open source platform development and provided the commercial .NET stacks for both the iOS and Android platforms. The .NET for iOS is called MonoTouch, or Xamarin.iOS, and .NET for Android is called Mono for Android, or Xamarin.Android.

    Xamarin frameworks enable developers to write cross-platform mobile applications targeting different platforms, including iOS, Android, and Windows Phone. Using Xamarin, you can develop a pure naive Android or iOS application using the C# programming language and share the application logic between the different platforms. This results in a faster development cycle and developers can leverage the existing C# and .NET programming skills, which helps reduce the learning curve to develop the mobile application.

    This book is structured in a logical sequence to help C# and .NET developers to build Xamarin.Android applications from the ground up. It explains the widely used basic and advanced Android concepts, including a user interface, data storage, consuming web services, geolocation, map, camera, and the build distribution process.

    This book provides the most comprehensive explanations of the basic and advanced Xamarin.Android concepts; you can precisely build with practical live examples to develop a complete working application. Over the course of this book, you will build a single application, the POIApp. With this application, we will cover all the fundamentals of Xamarin.Android to help you get stated with your own application development.

    What this book covers

    Chapter 1, The Anatomy of an Android App, provides an overview of the Android platform and what Android apps are composed of.

    Chapter 2, The Xamarin.Android Architecture, provides an overview of the Xamarin platforms and describes how the Mono and Android runtime work together in order to allow developers to build Android apps using C#.

    Chapter 3, Creating the Points of Interest App, walks you through how to set up the development environment, create a new Xamarin.Android app, and run the app in the Android emulator.

    Chapter 4, Adding a List View, describes Android's AdapterView architecture and walks you through how to use ListView and create a custom adapter. This chapter also covers how to download the data asynchronously from a web service and display the response on a custom ListView.

    Chapter 5, Adding a Details View, walks you through how to create a details view to display the details of POIApp, add navigation from ListView, and add actions to perform the save and delete web service operations.

    Chapter 6, Making Your App Orientation-aware, walks you through how to detect the device orientation and handle application behaviors on the configuration changes.

    Chapter 7, Designing for Multiple Screen Sizes, introduces you to the Android fragments and the different techniques used for managing the resources and layout to support multiple screen sizes, including Android tablets.

    Chapter 8, Creating Data Storage Mechanisms, discusses a number of available data storage options in Xamarin.Android and stores the Point of Interest list fetched from a web service using the SQLite database engine to make the list accessible when a device is offline.

    Chapter 9, Making POIApp Location Aware, discusses the various options that developers have in order to make their apps location-aware, and this chapter also covers how to add logic to determine a device's location, the address of a location, and display a location in the map app.

    Chapter 10, Adding the Camera App Integration, discusses the various options for integrating with a device camera to capture a picture of POIApp and upload the captured photo to a web service using the HTTP multipart form upload.

    Chapter 11, Publishing an App to the App Store, discusses the various options for distributing Android apps and also covers how to prepare a Xamarin.Android app for distribution.

    What you need for this book

    All the examples in this book can be completed using a 30-day trial version of Xamarin.Android. The examples were developed using Mac OS X (Yosemite), Xamarin Studio 5.9.3, and Xamarin.Android 5.1.3 (trial edition). Any later versions should work fine as long as they are valid Xamarin configurations. You can check the Xamarin website for specifics.

    Xamarin.Android can also be used in other configurations, including Windows OS. In Windows OS, you can either use Xamarin Studio or the Visual Studio Xamarin plugin as an IDE of your choice. Using a different configuration from what was used while developing the examples of this book may result in slight variations in the screens or steps described in the book.

    The examples provided in this book consume the REST web service developed in Java JAX-RS. You can deploy the web service code on your system to perform end-to-end testing, or alternatively, you can use the Apiary mock feed URLs provided in the code bundle. To deploy the web service code, you need MySQL and the Apache Tomcat™ application server.

    Who this book is for

    This book for C# and .NET developers with a desire to develop Android apps using their existing skill sets. This book includes a step-by-step approach to build an Android application using the Xamarin platform which will be valuable whether you're an experienced mobile developer or making your first push.

    It is assumed that you have some experience in software development and are familiar with the basic object-oriented development concept and practices. An understanding of C# syntax is a requirement and a good working knowledge of C# is a distinct advantage, though it is strictly not necessary.

    Conventions

    In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

    Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: These constants are placed in a Java source file named R.java.

    A block of code is set as follows:

    public override bool OnCreateOptionsMenu(IMenu

    Enjoying the preview?
    Page 1 of 1