The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #12 - Displaying the Book

At this point, you are probably wondering when we are ever going to have our digital book reader let us read a digital book.

Now, in this tutorial, your patience will be rewarded.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Note that starting in this tutorial, it is assumed that you know how to add import statements as needed as we refer to new classes in existing code, and so the required imports are not always going to be specified.

Step #1: Adding a Book

First, we need a book. Expecting you to write a book as part of this tutorial would seem to be a bit excessive. So, instead, we will use an already-written book: The War of the Worlds, by H. G. Wells, as distributed by Project Gutenberg.

EDITOR’S NOTE: We realize that this choice of book may be seen as offensive by Martians, as it depicts them as warlike invaders with limited immune systems. Please understand that this book is a classic of Western literature and reflects the attitude of the times. If you have any concerns about this material, please contact us at martians-so-do-not-exist@commonsware.com.

Download http://misc.commonsware.com/WarOfTheWorlds.zip and unpack its contents (a book/ directory of files) into your assets/ folder of your project. Windows and Linux Android Studio users can drag this book/ directory into the project and drop it in assets/ to copy the files to the proper location. You should wind up with assets/book/ and files inside of there:

Android Studio Project Explorer, Showing assets/book/
Figure 292: Android Studio Project Explorer, Showing assets/book/

In that directory, you will find some HTML and CSS files with the prose of the book, plus a contents.json file with metadata. We will examine this metadata in greater detail in the next section.

Step #2: Creating a ModelFragment

This sample project will use the “model fragment” pattern to hold onto the data about the book to be viewed. The “model fragment” pattern works well for cases where:

Something has to load that BookContents, ideally in the background, since reading an asset and parsing the JSON will take time. Also, something has to hold onto that BookContents, so it can be used from EmPubLiteActivity and the various chapter fragments in the ViewPager.

To that end, we will create a new class, cunningly named ModelFragment.

Right-click over the com.commonsware.empublite package in your java/ directory and choose New > Java Class from the context menu. Fill in ModelFragment as the name, android.app.Fragment as the superclass, and click OK to create the empty class.

Step #3: Defining Our Model

The preview of this section is presently indisposed.

Step #4: Examining Our Model

The preview of this section was abducted by space aliens.

Step #5: Defining Our Event

The preview of this section is [REDACTED].

Step #6: Loading Our Model

The preview of this section left for Hollywood to appear in a reality TV show.

Step #7: Registering for Events

The preview of this section was traded for a bag of magic beans.

Step #8: Adapting the Content

The preview of this section was eaten by a grue.

Step #9: Showing the Content When Loaded

The preview of this section was traded for a bag of magic beans.

Step #10: Attaching our ModelFragment

The preview of this section is in an invisible, microscopic font.

Step #11: Showing the Content After a Configuration Change

The preview of this section is sleeping in.

Step #12: Setting Up StrictMode

The preview of this section is [REDACTED].

In Our Next Episode…

The preview of this section was lost in the sofa cushions.