Book Image

Android 3.0 Application Development Cookbook

By : Kyle Merrifield Mew
Book Image

Android 3.0 Application Development Cookbook

By: Kyle Merrifield Mew

Overview of this book

<p>Android is a mobile operating system that runs on a staggering number of smartphones and tablets. Android offers developers the ability to build extremely rich and innovative applications written using the Java programming language. Among the number of books that have been published on the topic, what&rsquo;s missing is a thoroughly practical, hands-on book that takes you straight to getting your job done without boring you with too much theory.<br /><br />Android 3.0 Application Development Cookbook will take you straight to the information you need to get your applications up and running. This book is written to provide you with the shortest possible route between an idea and a working application. <br /><br />Work through the book from start to finish to become an Android expert, or use it as a reference book by applying recipes directly to your project.<br /><br />This book covers every aspect of mobile app development, starting with major application components and screen layout and design, before moving on to how to manage sensors such as internal gyroscopes and near field communications. Towards the end, it delves into smartphone multimedia capabilities as well as graphics and animation, web access, and GPS. <br /><br />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 3.</p>
Table of Contents (18 chapters)
Android 3.0 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a custom component


Not only can we define widgets in XML, set their properties, and generally manipulate and edit them at runtime, but it is also possible to create widgets entirely from scratch by extending existing views (or subclasses) and overriding their methods.

Getting ready

We are going to create a custom widget solely from within Java, so start a new Android project in Eclipse and open up the main Java activity.

How to do it...

  1. Inside our new activity, add a new inner class called MyCustomView that extends the View class. It is always a good idea to get into the habit of declaring instances as static where possible, because memory leaks on a battery-operated device have a far greater impact than they do on a PC:

    private static class MyCustomView extends View {
    
    }
  2. Give the new class a constructor:

    public MyCustomView(Context context) {
    
      super(context);
    
    }
  3. Now add and initialize a Paint field to this new class:

    private static class MyCustomView extends View {
    
      final Paint paint...