• res/layout-port-stylus-12key-480x320

• res/layout-port-stylus-nokeys-640x480

• res/layout-port-stylus-nokeys-480x320

• res/layout-port-finger-qwerty-640x480

• res/layout-port-finger-qwerty-480x320

• res/layout-port-finger-12key-640x480

• res/layout-port-finger-12key-480x320

• res/layout-port-finger-nokeys-640x480

• res/layout-port-finger-nokeys-480x320

• res/layout-land-notouch-qwerty-640x480

• res/layout-land-notouch-qwerty-480x320

• res/layout-land-notouch-12key-640x480

• res/layout-land-notouch-12key-480x320

• res/layout-land-notouch-nokeys-640x480

• res/layout-land-notouch-nokeys-480x320

• res/layout-land-stylus-qwerty-640x480

• res/layout-land-stylus-qwerty-480x320

• res/layout-land-stylus-12key-640x480

• res/layout-land-stylus-12key-480x320

• res/layout-land-stylus-nokeys-640x480

• res/layout-land-stylus-nokeys-480x320

• res/layout-land-finger-qwerty-640x480

• res/layout-land-finger-qwerty-480x320

• res/layout-land-finger-12key-640x480

• res/layout-land-finger-12key-480x320

• res/layout-land-finger-nokeys-640x480

• res/layout-land-finger-nokeys-480x320

Don’t panic! We will shorten this list in just a moment!

Note that for many of these, the actual layout files will be identical. For example, we only care about touch-screen layouts being different than the other two layouts, but since we cannot combine those two, we would theoretically have to have separate directories with identical contents for finger and stylus.

Also note that there is nothing preventing you from also having a directory with the unadorned base name (res/layout). In fact, this is probably a good idea, in case future editions of the Android runtime introduce other configuration options you did not consider — having a default layout might make the difference between your application working or failing on that new device.

Now, we can “cheat” a bit, by decoding the rules Android uses for determining which, among a set of candidates, is the “right” resource directory to use:

1. First up, Android tosses out ones that are specifically invalid. So, for example, if the screen size of the device is 320?240, the 640x480 directories would be dropped as candidates, since they specifically call for some other size.

2. Next, Android counts the number of matches for each folder, and only pays attention to those with the most matches.

3. Finally, Android goes in the order of precedence of the options — in other words, it goes from left to right in the directory name.

So we could skate by with only the following configurations:

• res/layout-port-notouch-qwerty-640x480

• res/layout-port-notouch-qwerty

• res/layout-port-notouch-640x480

• res/layout-port-notouch

• res/layout-port-qwerty-640x480

• res/layout-port-qwerty

• res/layout-port-640x480

• res/layout-port

• res/layout-land-notouch-qwerty-640x480

• res/layout-land-notouch-qwerty

• res/layout-land-notouch-640x480

• res/layout-land-notouch

• res/layout-land-qwerty-640x480

• res/layout-land-qwerty

• res/layout-land-640x480

• res/layout-land

Here, we take advantage of the fact that specific matches take precedence over “unspecified” values. So, a device with a QWERTY keyboard will choose a resource with qwerty in the directory over a resource that does not specify its keyboard type. Combine that with the “most matches wins” rule, we see that res/layout- port will only match devices with 480?320 screens, no QWERTY keyboard, and a touch-screen in portrait orientation.

We could refine this even further, to only cover the specific devices we are targeting (the T-Mobile G1, the Fictional One, and the Fictional Two), plus take advantage of res/layout being the overall default:

• res/layout-port-notouch-640x480

• res/layout-port-notouch

• res/layout-land-notouch-640x480

• res/layout-land-notouch

• res/layout-land

• res/layout

Here, 640x480 differentiates the Fictional One from the other two devices, while notouch differentiates the Fictional Two from the T-Mobile G1.

CHAPTER 20

Managing and Accessing Local Databases

SQLite[20] is a very popular embedded database, as it combines a clean SQL interface with a very small memory footprint and decent speed. Moreover, it is public domain, so everyone can use it. Lots of firms (Adobe, Apple, Google, Sun, Symbian) and open-source projects (Mozilla, PHP, Python) all ship products with SQLite.

For Android, SQLite is “baked into” the Android runtime, so every Android application can create SQLite databases. Since SQLite uses a SQL interface, it is fairly straightforward to use for people with experience in other SQL-based databases. However, its native API is not JDBC, and JDBC might be too much overhead for a memory- limited device like a phone, anyway. Hence, Android programmers have a different API to learn — the good news is that it is not very difficult.

This chapter will cover the basics of SQLite use in the context of working on Android. It by no means is a thorough coverage of SQLite as a whole. If you want to learn more about SQLite and how to use it in environments other than Android, a fine book is The Definitive Guide to SQLite[21] by Mike Owens (Apress, 2006).

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату