views:

919

answers:

3

I have a ListView powered by a custom adapter that dynamically changes the background color of each row based on its contents. The getView looks like this:

 @Override
 public View getView(int position, View convertView, ViewGroup parent)
 {
  View row = convertView;
  Power power = powers.get(position);
  if(row == null)
  {
   row = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
  }

  TextView text1 = (TextView)row.findViewById(android.R.id.text1);
  TextView text2 = (TextView)row.findViewById(android.R.id.text2);

  text1.setText(power.name);
  text2.setText(power.getAttackLine());

  row.setBackgroundColor(0xFF0000FF);

  return row;
 }

All that works peachy, except that whenever I select an item from the list, the standard red/orange highlight only shows up behind the row, only visible for a few pixels on each side of it. Is there a way to get the selection to show up on top of the background?

+1  A: 

Try android:drawSelectorOnTop="true" in the <ListView> element in your layout.

CommonsWare
Won't that just make the entire row orange, since the default ListView selector has no transparency?
Daniel Lew
I had kinda assumed he was already changing the selector. But, you are correct -- you'll need an selector nine-patch or XML-defined drawable that has appropriate transparency.
CommonsWare
Oh, fantastic! I am currently using the default selector, but I can change it easy enough. Thanks so much!
Ian
+1  A: 

I think the problem is that you're using simple_list_item_2, which has some padding/margin built into it. I recommend making your own version of simple_list_item_2, wherein you have both the height and the width set to fill_parent. You can see the latest simple_list_item_2 here.

Daniel Lew
Those few pixels actually kind of saved me, because they were the only way I was able to tell that the selector was being drawn beneath the row, as opposed to not being drawn at all. You're right, though; I do need to make my own view for the rows. Thanks for your help!
Ian
A: 

I have this problem too when I have changed the background of my rows in the code.

I have one row white, another gray (even/odd rows).

And when I try to select a row, I have the same issue as the first poster (the higlight only shows up behind the row, only visible for a few pixels on each side of it).

I am using my own list row layout like this:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/channel_row"
    android:layout_height="fill_parent">

So my width and height are both fill_parent. What can I do to show my default listSelector?

Thanks,

Wouter

wouter88