How and when do you make the transition to Android Ice Cream Sandwich (API 14+)? That’s a question I have been asking myself for existing apps and new ones. The relative market share of ICS is still very small — under 4%, as shown in the current report on distribution of devices by Android platforms. Still, I want to be looking ahead so there is less work to do at the time when ICS becomes the Android platform with the largest share.
What I intend to do for existing apps is stick with the themes and styles I had been using for pre-ICS (2.x) devices. For ICS devices, I want to move right away to the new themes provided with Ice Cream Sandwich. The simplest example of how to do this is the HelloWorld app that you get when you create a new Android project in Eclipse. By adding the right style definitions for theme in the layout folders, you can easily get an app with a title bar that is correct for the target platform. Figure 1 shows the Hello app on a pre-ICS device. Figure 2 shows the app on an ICS device.
Figures 1 – 2
I consider this a reasonable first step toward Ice Cream Sandwich. A few other steps are suggested in the last section below.
First Step – Defining the Theme
In order to set up the Hello World app to look right on ICS and pre-ICS devices, all you have to do is set up theme definitions in two styles.xml files. One goes in the regular res/values folder. The other goes in the special res/values-v14 folder, which indicates that is used only when the API level is 14 or greater.
values
styles.xml
values-v14
styles.xml
Inside each of styles.xml files is a definition for the resource named “MyTheme”. When the API level is 14 or higher, the style definition that is used is the one in values-14 styles.xml file. Here is what the two definitions look like:
File values/styles.xml contains:
<resources> <style name="MyTheme" parent="@android:style/Theme"> <!-- Any customizations for your app running on pre-3.0 devices here --> </style> </resources>
File values-v14/styles.xml contains:
<resources> <style name="MyTheme" parent="@android:style/Theme.DeviceDefault"></style> </resources>
The MyTheme style is used in the AndroidManifest to indicate what the default style is for activities in the application. The key line is the “android:theme” line inside the application definition.
<application android:theme="@style/MyTheme" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HelloWorldActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Note that there are two themes to try in ICS. There is “Theme.Holo.Light” and “Theme.DeviceDefault”. The Holo.Light theme gets you a title bar that has a light background.
All of this is very well explained here: Holo Everywhere (in the section “Using Holo While Supporting 2.x”). The author says, ”Most Android developers will still want to support 2.x devices for a while as updates and new devices continue to roll out.”. That’s an important point.
Source Code
The source code for this demo application is available in two places: (1) download from Google Docs; (2) download from Wglxy.com. The Google Docs zip file is a shared file, which means it should be available for everyone. Still, if you find that you cannot download it from Google Doc, try the second location.
This application has been compiled with API level 15 (4.0.3). It runs on devices and in the emulator from API level 8 to API level 15. If you start your app this way, be sure to do extensive testing at the lower API levels. You want to make sure that you are not using features introduced in API 15 that are not supported in older versions.
Next Steps
Following this note gets you started with Ice Cream Sandwich (ICS). You will have a Hello World app that you can use as a starter for other apps. Each of the apps will look like it is an app designed for ICS, at least as far as the title bar is concerned. And for the earlier API levels, it will look just the way you want it there too.
Once you have taken the first step, what should you do next? I don’t have a lot of suggestions to make at this time. What I intend to do is study more of the new features in ICS, including fragments, action bars, etc. I have a found a few resources that might help:
- Action Bar Compatibility - a compatibility library you can use if you want to build an app that looks the same, no matter whether it is API 8 or API15. The library supports a subset of ICS features.
- ActionBarSherlock
If you want the new ICS look and many of the new features of ICS, you should take a look at this package. It makes it so your app looks like an ICS app, even if it is running on an API level before 4.0.
I have to admit that this is a very simple demo app. What is interesting for me is how many times I have had to redo this first step. Between my work projects and my personal projects, I have done this about five times already. Each time I have to start searching the web and the good starting point (the Holo Everywhere note) never seems to make an impression. Whenever I find something where I cannot remember the solution, even a simple one, I know it’s time to write it down and put it in a place where I can find it easily with a Google search.



Recent Comments