This post has been moved to my new blog.
Java enum is powerful. However, it is tricky if you want to use it as integer just like in C/C++. Actually, it is not easy at all because Java enum elements are objects. So it is best to use Java enum as enum. But what about the case you need to pass it back an forth as an integer. One example is when you want to use enum for a ‘what’ of Android Message. Lets say you have defined the enum, WhatAbout, like this:
enum WhatAbout { ART, LIFE, MONEY, WORLD };
And you want to sendMessage to someHandler like this:
someHandler.sendMessage(updater.obtainMessage(WhatAbout.ART.ordinal(), 0, 0));
As you can see enum’s ordinal() method solves the issue. Now look at the receiver side. Here is the code for handleMessge() method of someHander
public void handleMessage(Message msg) { if (msg.what == WhatAbout.ART.ordinal()) { // Do something about ART } else if (msg.what == WhatAbout.LIFE.ordinal()) { // Do something about LIFE } else ... }
It will work, but looks ugly. Using switch must be better. However, unfortunately, the ordinal() method cannot be a case expression because it is not a constant expression. So here is my trick to get around that restriction and use switch:
static WhatAbout[] wa = WhatAbout.values(); public void handleMessage(Message msg) { try { switch(wa[msg.what]) { case ART: // Do something about ART break; case LIFE: // Do something about LIFE break; ... } } catch (Exception x) { // Handle ArrayIndexOutOfBoundsException exception } }
The point is having an enum value array instantiated and use it in the switch expression.
While I was following the Android instruction I ran into a couple of difficulties. So here is the list of them with how I resolved.
Forcing WUBI to install 32bit version.
I used Ubuntu Windows Installer (WUBI). It really simplified Windows-Ubuntu dual-boot setup. However, on my desktop it automatically choose 64bit Ubuntu without asking me. According to Android instruction 32-bit Ubuntu is preferred. So, I had to use command line option to install 32-bit Ubuntu using WUBI. From Windows command prompt, run
> wubi.exe --32bit
For more on WUBI: Wubi Guide.
Couldn’t find package git-core
Right after Ubuntu installation, if you follow Android instruction to install required packages then this error may occur. Then run this:
> sudo apt-get update
Couldn’t find package sun-java5-jdk
This will occur because Ubutu 10 comes with jdk 6. I followed the instruction on http://zebardast.ir/en/installing-sun-jdk-5-on-ubuntu-9-10-and-10-04/.
Making sure PATH includes ~/bin
Once ~/bin is created, at home run:
> source .profile > echo $PATH
Now, following Android instruction was good enough to finish the task.
I looked for how-to. And found one that uses API: http://www.anddev.org/hide_statusbar-title_on_qvga_-_getting_screen_resolution-t2689.html. This solution is nice when app window style needs to be determined and set dynamically.
Later, I found an another way that uses style and much easier if app window style is fixed. It is just adding the android:theme attribute to activity element in AdroidManifest.xml file:
<activity android:name=".HelloWorld" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
For more info, check the last part of http://developer.android.com/guide/topics/ui/themes.html and the Android Styles and Themes XML files.