Android Iconified List
![]()
I’ve had a lot of fun hacking on Android applications recently and I’m really looking forward to seeing the future of Android unfold. My first project was the the password safe I mentioned in my last post and while working on some features for it I was looking for an example of a composite view that allowed you have cute little icons next to each entry in a list, I found a few examples that did similar things, but nothing was quite what I was looking for so I threw together the view you see here last night. I’m sure someone is else is looking to do something similar so I thought I would post about it and make it available to everyone.
Most of the magic happens by in the BulletedText View class. I basically just extend LinearLayout and create a composite view that contains an ImageView and TextView object: (Sorry if the code is a little cramped, I need to figure out how to keep wordpress from eating blank lines)
public class BulletedTextView extends LinearLayout {
private TextView mText;
private ImageView mBullet;
public BulletedTextView(Context context, String text, Drawable bullet) {
super(context);
this.setOrientation(HORIZONTAL);
mBullet = new ImageView(context);
mBullet.setImageDrawable(bullet);
// left, top, right, bottom
mBullet.setPadding(0, 2, 5, 0);
addView(mBullet, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mText = new TextView(context);
mText.setText(text);
addView(mText, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void setText(String words) {
mText.setText(words);
}
public void setBullet(Drawable bullet) {
mBullet.setImageDrawable(bullet);
}
}
Then I created a custom list adapter for my view.
public class BulletedTextListAdapter extends BaseAdapter {
/**
* Remember our context so we can use it when constructing views.
*/
private Context mContext;
private List mItems;
public BulletedTextListAdapter(Context context) {
mContext = context;
mItems = new ArrayList();
}
public void addItem(BulletedText bt) {
mItems.add(bt);
}
public void setListItems(List bti) {
mItems = bti;
}
public int getCount() {
return mItems.size();
}
public Object getItem(int position) {
return mItems.get(position);
}
public boolean areAllItemsSelectable() {
return false;
}
public boolean isSelectable(int position) {
return mItems.get(position).isSelectable();
}
public long getItemId(int position) {
return position;
}
/**
* Make a BulletedTextView to hold each row.
*/
public View getView(int position, View convertView, ViewGroup parent) {
BulletedTextView btv;
if (convertView == null) {
btv = new BulletedTextView(mContext,
mItems.get(position).getText(),
mItems.get(position).getBullet());
} else {
btv = (BulletedTextView) convertView;
btv.setText(mItems.get(position).getText());
btv.setBullet(mItems.get(position).getBullet());
}
return btv;
}
}
That’s pretty much all there is to it! The full source code is available if you would like to download it. It is licensed under the Apache 2 open source license. I welcome any ideas for improvements!
Android : Des icônes pour vos listes 9:31 am on November 22, 2007 Permalink |
[...] Android Password Safe, Steven Osborn vient de publier un bout de code permettant de rajouter des icônes à coté des items d’une liste. Ce n’est certes pas [...]
Googlephone X Team » Blog Archive » Пример: список с иконками 4:40 am on November 23, 2007 Permalink |
[...] данном примере Steve Osborn показывает как можно изменить стандартный список, [...]
Android After All » Tutorial : l’explorateur système [Maj] 3:44 pm on November 27, 2007 Permalink |
[...] tutorial sur l’explorateur système. Il inclue l’utilisation des listes avec icônes, publié par un bloggeur après une que la question fût posée sur le google [...]
VataMyday 8:35 pm on May 22, 2009 Permalink |
any changes coming ?
steven 10:52 pm on May 28, 2009 Permalink |
VataMyday, I’m afraid not. I haven’t had much interest lately in Android development and haven’t put much thought into it.