Friday, February 5, 2010

Android Antics

Android's gui toolkit is, programmatically, nothing new and is pretty nice to work with given a phone. It's got a few problems though in how things work. Now stackoverflow is a great place to look to find answers to common programming problems ontop of #android-dev on freenode. But this simple problem I had yesterday and its simple solution took me so long to work through (at least 3 hours?). I hate these types of problems, the solution is simple yet you cannot find the answer cleanly in documentation or otherwise on the internet. So since it seems like I'm the first one to notice the problem in question, I will write about it here.
What is the problem?
Well, for multichoice listviews, you often want a checkbox. The checkbox is merely decoration though as the listview itself keeps all stateful information on being selected. Anyway, a checkbox is focusable and clickable. Because it has these attributes, it can automatically steal the onclick events from the entire listcell. You can solve this by setting the focusable attribute to false. This is where everybody else on the internet stops, and wrongly so because if a naughty user selects just the checkbox in a click, it will only fire the checkbox's events and not the listviews. Not to mention the state of the checkbox is then screwed up wrt the listview's. The solution after many maddening hours of trying different methods to have the checkbox "click" the listitem (which at the very least has a drawback of not highlighting the selection on press).. simply disable the clickable attribute that the button inherits from View.

So in summary, and hopefully so the searchengines catch it...
When putting buttons, checkables, or checkboxes into a listview's cell, set the focusable and clickable properties to false so the listview works properly.

No comments:

Post a Comment