Book Image

Android 9 Development Cookbook - Third Edition

By : Rick Boyer
Book Image

Android 9 Development Cookbook - Third Edition

By: Rick Boyer

Overview of this book

The Android OS has the largest installation base of any operating system in the world. There has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! With this extensively updated cookbook, you'll find solutions for working with the user interfaces, multitouch gestures, location awareness, web services, and device features such as the phone, camera, and accelerometer. You also get useful steps on packaging your app for the Android Market. Each recipe provides a clear solution and sample code you can use in your project from the outset. Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android Pie.
Table of Contents (24 chapters)
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Preface
Index

Creating a custom component


As we have seen in previous recipes, the Android SDK provides a wide range of components. But what happens when you can't find a prebuilt component that fits your unique needs? You can always create your own!

In this recipe, we will walk through creating a custom component that derives from the View class, just like the built-in widgets. Here's a high-level overview:

  1. Create a new class that extends View.
  2. Create custom constructor(s).
  3. Override onMeasure(), as the default implementation returns a size of 100 x 100.
  4. Override onDraw(), as the default implementation draws nothing.
  5. Define custom methods and listeners (such as the onClick() event).
  6. Implement custom functionality.

Note

Overriding onMeasure() and onDraw() is not strictly required, but the default behavior is likely not what you would want.

Getting ready

Start a new project in Android Studio and call it CustomView. Use the default wizard options, including the Phone & Tablet SDK and select Empty Activity when...