Android user interface development(2011)
Android User Interface Development Beginner's Guide Quickly design and develop compelling user interfaces for your Android applicaons Jason Morris BIRMINGHAM - MUMBAI D ownload f r o m W o w ! e B o o k < w w w . w owebook.com> 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 transmied in any form or by any means, without the prior wrien permission of the publisher, except in the case of brief quotaons embedded in crical arcles or reviews. Every eort has been made in the preparaon of this book to ensure the accuracy of the informaon presented. However, the informaon 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 informaon about all of the companies and products menoned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this informaon. First published: February 2011 Producon 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 ( charwak86@gmail.com ) Credits Author Jason Morris Reviewers David J. Groom Marn Skans Acquision Editor Chaitanya Apte Development Editor Reshma Sundaresan Technical Editor Harshit Shah Copy Editor Neha Shey Indexer Tejal Daruwale Editorial Team Leader Akshara Aware Project Team Leader Priya Mukherji Project Coordinator Shubhanjan Chaerjee Proofreader Joel T. Johnson Graphics Nilesh R. Mohite Producon Coordinators Kruthika Bangera Aparna Bhagat Cover Work Kruthika Bangera About the Author Jason Morris has worked on soware as diverse as fruit tracking systems, insurance systems, and travel search and booking engines. He has been wring soware for as long as he can remember. He is currently working as a Soware Architect for Travelstart in South Africa. He works on mulple 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 wring 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 Marn Skans for his invaluable input. About the Reviewer Marn Skans graduated from Lund University in Sweden, with a Master's degree in Computer Science. Aer a couple of years in the online markeng 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 plaorm which has been recently launched for the mobile market. www.PacktPub.com Support les, eBooks, discount offers and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt oers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com , you can also read a collecon of free technical arcles, sign up for a range of free newsleers and receive exclusive discounts and oers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant soluons to your IT quesons? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's enre 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 enrely free books. Simply use your login credenals for immediate access. Table of Contents Preface 1 Chapter 1: Developing a Simple Acvity 11 Developing our rst example 11 Creang the project structure 12 Time for acon – seng up the Android SDK 12 Time for acon – starng a new project 13 Examining the Android project layout 14 Time for acon – running the example project 14 The screen layout 15 The layout XML le 16 Resource selecon qualiers 16 Time for acon – seng up the queson acvity 18 Populang a View and a ViewGroup 19 Time for acon – asking a queson 19 Time for acon – adding a space for answers 21 Time for acon – adding more buons 23 Dening common dimensions 25 Limitaons of the layout XML format 27 Populang the QuesonAcvity 29 Time for acon – wring more Java code 30 Dynamically creang widgets 32 Time for acon – pung the quesons on the screen 32 Handling events in Android 34 Summary 36 Chapter 2: Presenng Data for Views 37 Lisng and selecng data 38 ListView choice modes 38 No selecon mode – CHOICE_MODE_NONE 38 Single selecon mode – CHOICE_MODE_SINGLE 39 Mulple selecon mode – CHOICE_MODE_MULTIPLE 40 Table of Contents [ ii ] Adding header and footer widgets 40 Creang a simple ListView 41 Time for acon – creang a fast food menu 41 Styling the standard ListAdapters 43 Dening standard dimensions 43 Time for acon – improving the restaurant list 44 Creang custom adapters 47 Creang a menu for The Burger Place 47 Time for acon – creang a Burger item layout 48 Time for acon – presenng Burger objects 50 Creang TheBurgerPlaceAcvity class 52 Time for acon – implemenng TheBurgerPlaceAcvity 53 Registering and starng TheBurgerPlaceAcvity 54 Using the ExpandableListView class 56 Creang ExpandableListAdapter implementaons 57 Using the GridView class 58 Time for acon – creang the fruit icon 59 Displaying icons in a GridView 60 Time for acon – building the fruit menu 61 Time for acon – creang the FourBucketsAcvity 62 Summary 64 Chapter 3: Developing with Specialized Android Widgets 67 Creang a restaurant review applicaon 68 Time for acon – creang the roboc review project structure 68 Building a TabAcvity 70 Creang tab icons 70 Android tabs and icons 71 Implemenng the ReviewAcvity 72 Time for acon – wring the ReviewAcvity class 72 Time for acon – creang the Review layout 74 Working with switcher classes 75 Time for acon – turning on the TextSwitcher 76 Creang a simple photo gallery 78 Time for acon – building the Photos tab 79 Creang a thumbnail widget 80 Implemenng a GalleryAdapter 80 Time for acon – the GalleryAdapter 81 Time for acon – making the gallery work 83 Building the reservaon tab 86 Time for acon – implemenng the reservaon layout 86 Time for acon – inializing the reservaon tab 89 Table of Contents [ iii ] Time for acon – listening to the SeekBar 92 Time for acon – selecng date and me 93 Creang 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 Acvies and Intents 103 Exploring the Acvity class 104 Using Bundle objects 105 Time for acon – building an example game: "guess my number" 106 Creang and consuming intents 110 Dening Intent acons 111 Passing data in an Intent 112 Adding extra data to an Intent 112 Using advanced Intent features 113 Geng data back from an Intent 113 Time for acon – viewing phone book contacts 114 Summary 118 Chapter 5: Developing Non-linear Layouts 119 Time for acon – creang a layouts example project 120 FrameLayout 121 Common uses 121 Time for acon – developing a FrameLayout example 122 Table Layout 126 Common uses 127 Using TableLayout for a memory game 127 Time for acon – developing a simple memory game 128 AbsoluteLayout/Custom Layouts 133 Developing your own Layouts 134 Time for acon – creang a custom layout 134 Using the CircleLayout 137 Time for acon – nishing the CircleLayout example 137 RelaveLayout 140 Common uses 140 Integrang the RelaveLayout 141 Time for acon – creang a contact editor 141 Time for acon – integraon with the layout example 144 SlidingDrawer 146 Common uses 146