Book Image

Android Design Patterns and Best Practice

By : Kyle Mew
Book Image

Android Design Patterns and Best Practice

By: Kyle Mew

Overview of this book

Are you an Android developer with some experience under your belt? Are you wondering how the experts create efficient and good-looking apps? Then your wait will end with this book! We will teach you about different Android development patterns that will enable you to write clean code and make your app stand out from the crowd. The book starts by introducing the Android development environment and exploring the support libraries. You will gradually explore the different design and layout patterns and get to know the best practices of how to use them together. Then you’ll then develop an application that will help you grasp activities, services, and broadcasts and their roles in Android development. Moving on, you will add user-detecting classes and APIs such as gesture detection, touch screen listeners, and sensors to your app. You will also learn to adapt your app to run on tablets and other devices and platforms, including Android Wear, auto, and TV. Finally, you will see how to connect your app to social media and explore deployment patterns as well as the best publishing and monetizing practices. The book will start by introducing the Android development environment and exploring the support libraries. You will gradually explore the different Design and layout patterns and learn the best practices on how to use them together. You will then develop an application that will help you grasp Activities, Services and Broadcasts and their roles in Android development. Moving on, you will add user detecting classes and APIs such as at gesture detection, touch screen listeners and sensors to our app. You will also learn to adapt your app to run on tablets and other devices and platforms, including Android Wear, Auto, and TV. Finally, you will learn to connect your app to social media and explore deployment patterns and best publishing and monetizing practices.
Table of Contents (20 chapters)
Android Design Patterns and Best Practice
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

Welcome to Android Design Patterns and Best Practice, a comprehensive guide to how to get the most out of your apps with the tried and tested programming philosophy, design patterns. These patterns provide a logical and elegant approach to solving many of the development problems that coders face. These patterns act as a guide creating a clear path from problem to solution, and although applying a design pattern does not guarantee best practice in itself, it will hugely assist the process and make the discovery of design flaws far easier. Design patterns can be implemented on very many platforms and written in as many programming languages. Some code libraries even apply patterns as part of their internal mechanics, and many readers will already be familiar with the Java Observer and Observable classes. The Android SDK we will be exploring makes great use of many patterns, such as factories, builders and listeners (which are really just observer patterns). Although we will cover these built-in design patterns, the book will mostly explore how we can build our own, custom made, patterns and apply them to Android development. Rather than approach each design pattern in turn, this book approaches the subject from the perspective of a developer, moving through each aspect of app development exploring individual design patterns as they would arise in the course of building an Android app. To clarify this journey, we will be focusing on a single imaginary app, designed to support a small business. This will take us from application conception through to publication, covering such topics as UI design, internal logic and user interaction along the way. During each of these steps we will explore those design patterns that are relevant to that process, by first exploring the pattern in its abstract form and then applying it to that particular situation. By the end of the book you will have learned how design patterns can be applied to all aspects of Android development and how using them assists best practice. It is the concept of design patterns that is more important than any specific pattern itself. Patterns can, and should, be adapted to suit our specific purposes, and by learning this way of approaching app development, we can even go on to create entirely original patterns of our own.

What this book covers

Chapter 1, Design Patterns, introduces the development environment, and two common design patterns, the factory and abstract factory.

Chapter 2, Creational Patterns, covers material and interface design, exploring the design support library and the builder design pattern.

Chapter 3, Material Patterns, introduces Android User Interfaces and some of the most significant material design components such as the app bar and sliding navigation drawer. This will introduce menus and action icons and how to implement them and how to use a drawer listener to detect user activity.

Chapter 4, Layout Patterns, leads in from the previous one, delving further into Android Layout design patterns and how gravity and weight can be used to create layouts that work on a variety of devices. This will take us onto how Android handles device orientation and screen size and shape differences. The strategy pattern is introduced and demonstrated.

Chapter 5, Structural Patterns, delves us deeper into the design library and create a layout governed by a coordinator layout with a recycler view. This requires exploring the adapter design pattern, first the internal versions and then we build one of our own, as well as a bridge pattern and facade and filter patterns.

Chapter 6, Activating Patterns, shows us how to apply patterns directly to our app. We cover more design library features such as collapsing toolbars, scrolling and divider. We crate a custom dialog, triggered by user activity. We revisit the factory patterns and show how a builder pattern can be used to inflate a UI.

Chapter 7, Combining Patterns, introduces and demonstrates two new structural patterns the prototype and decorator, covering their flexibility. This is then put in to practice as we use the patterns to control a UI comprised of different compound buttons such as switches and radio groups.

Chapter 8, Composing Patterns, concentrates on the composite pattern and how it can be used in many situations and how to select the right situation. We then continue to use it in a practical demonstration to inflate a nested UI. This leads into the storage and retrieval of persistent data, using internal storage, application files and ultimately user settings in the form of shared preferences.

Chapter 9, Observing Patterns, looks at the visual processes involved in the transitions from one Activity to another and how this can be far more than mere decoration. The reader learns how to apply transitions and shared elements to efficiently use the minimal screen space of mobile devices and simplify application use and operation.

Chapter 10, Behavioral Patterns, concentrates on the major behavioral patterns, template, strategy, visitor and state. It provides working demonstrations of each and covers their flexibility and usage.

Chapter 11, Wearable Patterns, shows working of Android Wear, TV and Auto, demonstrating how to set up and configure each in turn. We examine the differences between these and the standard handheld app.

Chapter 12, Social Patterns, shows how to add web functionality and social media. Firstly the WebView is explored and how it can be used to create internal web-apps. Next, how to connect our app to Facebook is explored, showing how this is done and what we can do with it. The chapter concludes by examining other social platforms, such as Twitter.

Chapter 13, Distribution Patterns, covers deployment, publication and monetization of Android apps. The reader is led through the registration and publication process and we take a look at advertising options and which are best suited to which purpose. Finally we look at how we can maximize potential users with a few tips and tricks of deployment.

What you need for this book

Android Studio and SDK are both open source and can be installed from a single package. With one minor exception, which is outlined thoroughly in the relevant chapter, this is all the software required for this book.

Who this book is for

This book is intended for Android developers who have some basic android development experience. Basic Java programming knowledge is a must to get the most out of this book.

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: "Add three TextViews to your layout, and then add the code to your MainActivity's onCreate() method."

A block of code is set as follows:

Sequence prime = (Sequence) SequenceCache.getSequence("1");
primeText.setText(new StringBuilder()
        .append(getString(R.string.prime_text))
        .append(prime.getResult())
        .toString());

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    @Override
    public String getDescription() {
        return filling.getDescription() + " Double portion";
    }

Any command-line input or output is written as follows:

/gradlew clean:

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Enable developer options on your handset. On some models, this can involve navigating to Settings | About phone"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

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 for this book from your account at http://www.packtpub.com. 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Android-Design-Patterns-and-Best-Practice. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/AndroidDesignPatternsandBestPractice.pdf.

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.