Skip to content

Instantly share code, notes, and snippets.

@bartvandendriessche
Created July 31, 2010 12:18
Show Gist options
  • Select an option

  • Save bartvandendriessche/502120 to your computer and use it in GitHub Desktop.

Select an option

Save bartvandendriessche/502120 to your computer and use it in GitHub Desktop.
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(layout, null);
}
final TimeEntryRowModel entry = getItem(position);
if (entry != null) {
row.setBackgroundColor(getContext().getResources().getColor(ColorUtil.getRandomBlockColor(entry.getProjectName())));
// handle the comment button
row.findViewById(R.id.comment).setBackgroundColor(getContext().getResources().getColor(ColorUtil.getRandomDarkBlockColor(entry.getProjectName())));
// if there is a comment, change image to the dotted text bubble
if (entry.getEntry().getDescription() != null && !"".equals(entry.getEntry().getDescription())) {
((ImageView) row.findViewById(R.id.comment_icon)).setImageResource(R.drawable.comment_on);
}
// initialize time entry name and task name
((TextView) row.findViewById(R.id.time_entry_name))
.setText(String.format("%s [%s]", entry.getProjectName(), entry.getProjectCompanyName()));
((TextView) row.findViewById(R.id.time_entry_task))
.setText(entry.getTodoName());
// handle the hours text fields
((TextView) row.findViewById(R.id.time_tracking_total_hours))
.setText(Integer.toString(NumberFormatUtil.getDoubleIntegerUnits(entry.getEntry().getHours())));
((TextView) row.findViewById(R.id.time_tracking_total_hours_fraction))
.setText(Integer.toString(NumberFormatUtil.getDoubleDecimalUnits(entry.getEntry().getHours())));
}
return row;
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@drawable/block_purple_dark" />
<item android:color="@drawable/block_purple"/>
</selector>
@bartvandendriessche
Copy link
Author

Right, the solution to having ListView rows change colour based on their state is to just create a selector Drawable instead of a selector Color. Then call row.setBackgroundDrawable instead of row.setBackgroundColor during setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment