Wednesday, July 28, 2010

ListView.setEmptyView() gotcha

When you set a ListView's "empty view" programmatically, you can end up scratching your head as to why your empty view actually doesn't appear when the list is empty.

If this happens, then what you forgot is that you must manually add your empty view to your view hierarchy, cos ListView won't do it for you. Although it's obvious when you think about it, the documentation doesn't mention this detail, and Googling shows at least one person had the problem.

Here's the code with the lines that it's all too easy to forget in bold...

TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setText("This appears when the list is empty");
emptyView.setVisibility(View.GONE);
((ViewGroup)list.getParent()).addView(emptyView);
list.setEmptyView(emptyView);


Friday, July 23, 2010

Tip: Developing with a Samsung Galaxy S

It's amazingly non-obvious how to get development going with a Samsung Galaxy S. The USB driver in the Android SDK doesn't work (IIRC it only recognizes some subset of HTC models), and Samsung don't seem to ship one of their own anywhere. I spent a number of hours Googling and trying various broken drivers that did got me nowhere.

Until I found something that worked. The trick is to install a piece of Samsung software called "New PC Studio" (download from http://www.samsungmobile.co.uk/support/softwaremanuals/newPCStudio.do). Follow the install instructions, and run it up. When you connect your phone (after being prompted to by the app UI of course) it'll install a bunch of drivers for you.

Now, one of these drivers will fail to install, and New PC Studio will inform you that your phone is "unrecognized". Don't worry, because before it failed it successfully installed a working USB driver that Eclipse and ADB can use, which was all you wanted anyway. You can then switch back to Eclipse/ADB and ignore New PC Studio for the rest of your life.

Thursday, July 15, 2010

Views do not have an "android:enabled" attribute

So trying to use it for View-derived classes in your layout XML does nothing.

Trying to use non-existent attributes normally causes a compiler error, but not in the case of android:enabled. I'm guessing that this is because it is valid for a very few types not used in layout files - e.g., Menu, Application - and the schema isn't very tightly enforced.

Therefore, if you want a button or something to begin life disabled until some event occurs, you can't do that in XML... you have to do it in code, i.e. use setEnabled(false) in your onCreate().

(I found this out the hard way... spent two hours convinced I'd found a bug in StateListDrawable or because my apparently-disabled buttons were being drawn just as if they were enabled. Which of course they were. Doh!).