Head First Android Development

Head First Android Development

English | 2015 | ISBN: 978-1449362188 | 736 Pages | PDF, EPUB | 87 MB


If you have an idea for a killer Android app, this book will help you build your first working application in a jiffy. You’ll learn hands-on how to structure your app, design interfaces, create a database, make your app work on various smartphones and tablets, and much more. It’s like having an experienced Android developer sitting right next to you! All you need is some Java know-how to get started.

+

Android uses SQLite databases to persist data

All apps need to store data, and the main way you do that in Androidville is with a SQLite database. Why SQLite?

It’s lightweight.
Most database systems need a special database server process in order to work. SQLite doesn’t, a SQLite database is just a file. When you’re not using the database, it doesn’t use up any processor time. That’s important on a mobile device, because we don’t want to drain the battery.
We’re going to go through the basics of SQLite in this chapter.
It’s optimized for a single user.
Our app is the only thing that will talk to the database, so we shouldn’t have to identify ourselves with a username and password.
If you plan on doing a lot of database heavy lifting in your apps, we suggest you do more background reading on SQLite and SQL.
It’s stable and fast.
SQLite databases are amazingly stable. They can handle database transactions, which means if you’re updating several pieces of data and screw up, SQLite can roll the data back. Also, the code that reads and writes the data is written in optimized C code. Not only is it fast, but it also reduces the amount of processor power it needs.

Where’s the database stored?

Android automatically creates a folder for each app where the app’s database can be stored. When we create a database for the Starbuzz app, it will be stored in the following folder:

Three key layouts: relative, linear, and grid

Layouts come in several flavors, and each one has their own policy to follow when deciding where to position the views it contains. Here are three of the key ones. Don’t worry about the details for now, over the next few pages we’re going to take you through each one.
RelativeLayout
A relative layout displays its views in relative positions. You define the position of each view relative to other views in the layout, or relative to its parent layout. As an example, you can choose to position a text view relative to the top of the parent layout, a spinner underneath the text view, and a button relative to the bottom of the parent layout.

LinearLayout

A linear layout displays views next to each other either vertically or horizontally. If it’s vertically, the views are displayed in a single column. If it’s horizontally, the views are displayed in a single row.
When vve added our button to the layout, vve positioned the button so that it was underneath the text view, and so that the left edge of the button lined up with the left edge of the text view. We positioned the button relative to the text view, and this is reflected in the XML:
There are different ways of writing the layout XML in order to produce the same visual effect. As an example, the XML above specifies that the button is positioned below the text view. An equivalent statement would be to say that the text view is positioned above the button