I have a problem with a simple ListView on a Samsung phone.
I am creating a new ListView programmatically. Elements are simple TextView. I put the listener on my list:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { view.setSelected(true); } });
TextView (i.e. listView elements) use a ColorStateList as textColor (pressed β green, selected β blue, default β red).
Everything is fine on the emulator: by default, red objects when I press one, it turns green, and when I release it, it turns blue. If I select another item: the previously selected one will return to red, and the newly selected will turn blue.
On my Samsung device: by default, red elements turn red when I press one, it turns green, and when I release it, it turns red again (i.e. not selected).
This seems to be a bug in the implementation of Samsung ListView (it is a custom implementation and it is so difficult to track without source code).
EDIT : not an error, but slightly different behavior due to touch mode (see link in accepted answer)
Do you have any ideas on how to handle this error / behavior?
Additional limitation: I cannot use the xml selector because I get the color for use only at runtime.
My device is Samsung GT-B5330, API 15. (but I expect this to happen on most Samsung devices).
Here is the complete (compiled) code
import android.R; import android.app.Activity; import android.content.res.ColorStateList; import android.database.DataSetObserver; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; public class TestActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootLayout = new RelativeLayout(this); setContentView(rootLayout); //create listView ListView listView = new ListView(this); listView.setAdapter(new MyListAdapter()); listView.setDivider(null); listView.setDividerHeight(0); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setItemsCanFocus(false); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { view.setSelected(true); } }); listView.setBackgroundColor(Color.WHITE); //positionning listView RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,200); lp.setMargins(50,50,10,10); rootLayout.addView(listView, lp); } private class MyListAdapter implements ListAdapter{ @Override public View getView(int position, View convertView, ViewGroup parent) { ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{R.attr.state_pressed}, new int[]{R.attr.state_selected}, new int[]{-R.attr.state_selected}, }, new int[]{ Color.GREEN, Color.BLUE, Color.RED}); TextView textView = new TextView(parent.getContext()); textView.setText("Item " + position); textView.setTextColor(colorStateList); return textView; } @Override public boolean areAllItemsEnabled() { return true; } @Override public boolean isEnabled(int position) { return true; } @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getCount() { return 10; } @Override public Object getItem(int position) { return "data "+position; } @Override public long getItemId(int position) { return position; } @Override public boolean hasStableIds() { return true; } @Override public int getItemViewType(int position) { return position; } @Override public int getViewTypeCount() { return getCount(); } @Override public boolean isEmpty() { return getCount()>0; } } }