For a very long time I've wanted to do some android developement and last week I finally got started. And now I have something to show so I thought I'd do a little piece on it here on the blog.
A while a go we in the student corridor where I live talked about social games and that gave me the idea for my first title. It's an Android version of the game Taboo, where you get a certain word you got to describe together with a set of other words which you are not allowed to use. For example the word could be Stove and you wouldnt be allowed to say things like "cook", "pan", "pot" etc. I've made it so there are some categories which you can enable or disable and some settings to controll a timer for how long a word is valid.
Here is a screenshot from the main panel. A very simple approach to everything.
It wouldn't be a proper developer blog without some code to follow it up. There was one thing that took a while for me to figure out when making this application and that was the ability to use custom data in list views. I wanted to generate a bunch of checkboxes so you can enable and disable categories and the way to do that is to write your custom adapter.
package nu.silven.taboo; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import android.content.SharedPreferences; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; public class CheckBoxSettingsAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener { private List<String> items; private SharedPreferences preferences; public CheckBoxSettingsAdapter(Collection<String> items, SharedPreferences preferences) { this.preferences = preferences; this.items = new ArrayList<String>(items); Collections.sort(this.items); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = new CheckBox(parent.getContext()); } String category = items.get(position); CheckBox asBox = (CheckBox) convertView; asBox.setText(category); asBox.setOnCheckedChangeListener(this); asBox.setChecked(preferences.getBoolean(category, true)); return asBox; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { preferences.edit().putBoolean((String)buttonView.getText(), isChecked).commit(); Log.d("TABOO CATEGORIE", "The category (" + buttonView.getText() + ") is now " + (isChecked ? "checked" : "unchecked")); } }
The interesting part here lies in the getView() method. It is called by the ListView(?) in order to populate itself. We get a View item, potentiall null, and it excpects a View back. The reason you get one as an argument is because android wants you to reuse views sometimes. When the list is so long it doesn't fit the screen anymore for example. It makes no sense to keep all the views in memery when they're offscreen so it asks you to reuse them instead.
The next thing I do to set the OnCheckChangeListener to the Adapter, in order to change the SharedPreferences whenever a box is clicked.
Taboo is pretty finished and I'm working on filling it with words. After some extensive beta testing this could be available in the near future. With more and better android projects to come.