Android User Interface Development Beginner's Guide
Total Page:16
File Type:pdf, Size:1020Kb
Android User Interface Development Beginner's Guide Quickly design and develop compelling user interfaces for your Android applications Jason Morris Android User Interface Development Beginner's Guide Copyright © 2011 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: February 2011 Production Reference: 1160211 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849514-48-4 www.packtpub.com Cover Image by Charwak A ([email protected]) Credits Author Editorial Team Leader Jason Morris Akshara Aware Reviewers Project Team Leader David J. Groom Priya Mukherji Martin Skans Project Coordinator Acquisition Editor Shubhanjan Chatterjee Chaitanya Apte Proofreader Development Editor Joel T. Johnson Reshma Sundaresan Graphics Technical Editor Nilesh R. Mohite Harshit Shah Production Coordinators Copy Editor Kruthika Bangera Neha Shetty Aparna Bhagat Indexer Cover Work Tejal Daruwale Kruthika Bangera About the Author Jason Morris has worked on software as diverse as fruit tracking systems, insurance systems, and travel search and booking engines. He has been writing software for as long as he can remember. He is currently working as a Software Architect for Travelstart in South Africa. He works on multiple front-end and middleware systems, leveraging a variety of Java based technologies. The people I'd like to thank most for their direct, or indirect help in writing this book are my wife Caron Morris, my father Mike Morris, my mom Jayne Morris, and the rest of my family for their love and support. I'd also like to thank Wayne, Stuart, Angela, and James, and everyone on my team at Travelstart. Finally a very big thanks to Martin Skans for his invaluable input. About the Reviewer Martin Skans graduated from Lund University in Sweden, with a Master's degree in Computer Science. After a couple of years in the online marketing industry, he moved on to become a developer for Travelstart, an online travel agency. He relocated to Cape Town and is currently working on Travelstart's African travel platform which has been recently launched for the mobile market. www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. 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. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across 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 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 nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Developing a Simple Activity 11 Developing our first example 11 Creating the project structure 12 Time for action – setting up the Android SDK 12 Time for action – starting a new project 13 Examining the Android project layout 14 Time for action – running the example project 14 The screen layout 15 The layout XML file 16 Resource selection qualifiers 16 Time for action – setting up the question activity 18 Populating a View and a ViewGroup 19 Time for action – asking a question 19 Time for action – adding a space for answers 21 Time for action – adding more buttons 23 Defining common dimensions 25 Limitations of the layout XML format 27 Populating the QuestionActivity 29 Time for action – writing more Java code 30 Dynamically creating widgets 32 Time for action – putting the questions on the screen 32 Handling events in Android 34 Summary 36 Chapter 2: Presenting Data for Views 37 Listing and selecting data 38 ListView choice modes 38 No selection mode – CHOICE_MODE_NONE 38 Single selection mode – CHOICE_MODE_SINGLE 39 Multiple selection mode – CHOICE_MODE_MULTIPLE 40 Table of Contents Adding header and footer widgets 40 Creating a simple ListView 41 Time for action – creating a fast food menu 41 Styling the standard ListAdapters 43 Defining standard dimensions 43 Time for action – improving the restaurant list 44 Creating custom adapters 47 Creating a menu for The Burger Place 47 Time for action – creating a Burger item layout 48 Time for action – presenting Burger objects 50 Creating TheBurgerPlaceActivity class 52 Time for action – implementing TheBurgerPlaceActivity 53 Registering and starting TheBurgerPlaceActivity 54 Using the ExpandableListView class 56 Creating ExpandableListAdapter implementations 57 Using the GridView class 58 Time for action – creating the fruit icon 59 Displaying icons in a GridView 60 Time for action – building the fruit menu 61 Time for action – creating the FourBucketsActivity 62 Summary 64 Chapter 3: Developing with Specialized Android Widgets 67 Creating a restaurant review application 68 Time for action – creating the robotic review project structure 68 Building a TabActivity 70 Creating tab icons 70 Android tabs and icons 71 Implementing the ReviewActivity 72 Time for action – writing the ReviewActivity class 72 Time for action – creating the Review layout 74 Working with switcher classes 75 Time for action – turning on the TextSwitcher 76 Creating a simple photo gallery 78 Time for action – building the Photos tab 79 Creating a thumbnail widget 80 Implementing a GalleryAdapter 80 Time for action – the GalleryAdapter 81 Time for action – making the gallery work 83 Building the reservation tab 86 Time for action – implementing the reservation layout 86 Time for action – initializing the reservation tab 89 [ ii ] Table of Contents Time for action – listening to the SeekBar 92 Time for action – selecting date and time 93 Creating complex layouts with Include, Merge, and ViewStubs 96 Using Include tags 97 Merging layouts 97 Using the ViewStub class 99 Summary 100 Chapter 4: Leveraging Activities and Intents 103 Exploring the Activity class 104 Using Bundle objects 105 Time for action – building an example game: "guess my number" 106 Creating and consuming intents 110 Defining Intent actions 111 Passing data in an Intent 112 Adding extra data to an Intent 112 Using advanced Intent features 113 Getting data back from an Intent 113 Time for action – viewing phone book contacts 114 Summary 118 Chapter 5: Developing Non-linear Layouts 119 Time for action – creating a layouts example project 120 FrameLayout 121 Common uses 121 Time for action – developing a FrameLayout example 122 Table Layout 126 Common uses 127 Using TableLayout for a memory game 127 Time for action – developing a simple memory game 128 AbsoluteLayout/Custom Layouts 133 Developing your own Layouts 134 Time for action – creating a custom layout 134 Using the CircleLayout 137 Time for action – finishing the CircleLayout example 137 RelativeLayout 140 Common uses 140 Integrating the RelativeLayout 141 Time for action – creating a contact editor 141 Time for action – integration with the layout example 144 SlidingDrawer 146 Common uses 146 [ iii ] Table of Contents Creating a SlidingDrawer example 147 Time for action – creating a SlidingDrawer 147 Time for action – sliding drawer integration 148 Summary 150 Chapter 6: Validating and Handling Input Data 153 Dealing with undesirable input 153 Correctly labeling input 154 Signaling undesirable input 154 Recovering from undesirable input 155 Giving users direct feedback 155 Avoiding invalid input entirely 156 Capturing date and time 156 Using spinners and ListView for selection 159 Changing the data set 159 Disabling selections 159 Capturing text input 160 Autocompleting text input 160 Building activitiesor f results 162 Generic filtering search Activity 162 Time for action – creating the ListItemSelectionActivity 163 Time for action – creating an ArrayAdapter 164 Time for action – creating the CursorAdapter 165 Time for action – setting up the ListView 169 Time for action – filtering the list 170 Time for action – returning the selection 171 Using the ListItemSelectionActivity 172 Summary 174 Chapter 7: Animating Widgets and Layouts 175 Using standard Android animations 176