Android: Change ListView Row TextView Color When Checked

Author
Michael
Published
2010, Dec 28, 06:07 pm
Category
CodingAndroid
Tags

"For the past three days, I’ve been trying to make a checkable ListView where the checked status would be indicated by changing the color of a TextView within the LinearLayout which is the base view of each row. Which is to say, when I click a row, the row becomes checked and a textview in that row changes color to indicate the new state."

A picture says a thousand words.


For the past three days, I’ve been trying to make a checkable ListView where the checked status would be indicated by changing the color of a TextView within the LinearLayout which is the base view of each row. Which is to say, when I click a row, the row becomes checked and a textview in that row changes color to indicate the new state.

Multi Select

I finally succeeded and want to share. If things are unclear, feel free to ask.

public class Item {
|||private String name;
|||public boolean select;
|||...
}

In your class which extends ListActivity

protected void onListItemClick(ListView l, View v, int position, long id) {
|||Item selected = items.get(position);
|||if (selectMode) {
||||||selected.select = !selected.select;
||||||...
|||}
|||...
}

In values/colors.xml

<resources>
|||<color name="red">#f00</color>
|||<color name="white">#fff</color>
|||...
</resources>

In your custom adapter class

public View getView(int position, View convertView, ViewGroup parent) {
|||View v = convertView;
|||if (v == null) {
||||||v = inflater.inflate(R.layout.row, null);
|||}
|||Item i = items.get(position);
|||if (i != null) {
||||||TextView nameTV = ((TextView)v.findViewById(R.id.name));
||||||String name = i.getName();
||||||nameTV.setText(name);
||||||if (i.select) {
|||||||||nameTV.setTextColor(context.getResources().getColor(R.color.red));
||||||}
||||||else {
|||||||||nameTV.setTextColor(context.getResources().getColor(R.color.white));
||||||}
||||||...
|||}
|||return v;
}

As my listview rows are LinearLayouts I need a checkable one, so I took one from tokudu

public class CheckableLinearLayout extends LinearLayout implements Checkable {
|||private CheckedTextView _checkbox;
|||public CheckableLinearLayout(Context context, AttributeSet attrs) {
||||||super(context, attrs);
|||}
|||@Override
|||protected void onFinishInflate() {
||||||super.onFinishInflate();
||||||// find checked text view
||||||int childCount = getChildCount();
||||||for (int i = 0; i < childCount; ++i) {
|||||||||View v = getChildAt(i);
|||||||||if (v instanceof CheckedTextView) {
||||||||||||_checkbox = (CheckedTextView)v;
|||||||||}
||||||}
|||}
|||public boolean isChecked() {
||||||return _checkbox != null ? _checkbox.isChecked() : false;
|||}
|||public void setChecked(boolean checked) {
||||||if (_checkbox != null) {
|||||||||_checkbox.setChecked(checked);
||||||}
|||}
|||public void toggle() {
||||||if (_checkbox != null) {
|||||||||_checkbox.toggle();
||||||}
|||}
}

And the base view for your row should be a [package name].CheckableLinearLayout

p.s. Most posts around the Internet seem convinced that the right way to do this sort of thing involves xml and selectors, but for me that has been a nightmare. Doing things mostly in the code has been much happier (and more importantly, it’s actually working now).



Comments


Jeffrey 2012, Dec 14, 12:04 am

can you give the whole code??
and can you help me to solve how to compare two files in android?? using md5 and fileinputstream..
thanks...

FriendofWakey 2013, Apr 25, 01:38 pm

Thanks for posting Wakey, it saved me a lot of grief. My touch screen died on my DROID 3 and with Wakey and a combination of "adb" I was able to wake it up, "swipe" the screen and control it remotely to get my contacts backed up.

Add a comment