title: ListView selection custom color
body: <p>I've made my own custom theme but there are a few bits that the theme didn't color such as the ListView I'm working on so I'm doing those piece by piece.  I've done my homework with this and know about creating the drawable background selector XML file, but it's not working right.  Further I tried some other ways to force it but can't get it to work.  I'll show you the problem then follow that with my code.</p>
  
  <p>The "Pressed" color is working.  In this screenshot, I'm pressing and holding down an item:</p>
  
  <p><img src="https://i.stack.imgur.com/1CDz1.png" alt="Pressing ListView Item"></p>
  
  <p>However when I remove my finger, the selected item doesn't remain colored.  Note that checklist 1 is selected and displayed but no highlight:</p>
  
  <p><img src="https://i.stack.imgur.com/nbdl7.png" alt="Finger removed, ListView Item is selected but no color"></p>
  
  <p>This is my background drawable that I apply to each list item view:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  &lt;item android:state_selected="false" android:state_pressed="false"
      android:drawable="@android:color/transparent" /&gt;
  &lt;item android:state_pressed="true" android:drawable="@android:color/holo_orange_dark" /&gt;
  &lt;item android:state_selected="true" android:state_pressed="false"
      android:drawable="@android:color/holo_orange_light" /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>You'll note that "selected" should show "holo_orange_light" but does not.  The following code is where I assign the background.  I initially was instantiating an off the shelf ArrayAdapter.  In order to apply this background I descended from ArrayAdapter and added the code to the method that builds the view.</p>
  
  <p>Here is the customized ArrayAdapter:</p>
  
  <pre><code>private class CustomArrayAdapter extends ArrayAdapter&lt;Checklist&gt; {
  
      public CustomArrayAdapter(Context context, int resource, int textViewResourceId,
              List&lt;Checklist&gt; objects) {
          super(context, resource, textViewResourceId, objects);
  
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          convertView = super.getView(position, convertView, parent);
          convertView.setBackgroundResource(R.drawable.listview_background);
          return convertView;
      }
  
  }
  </code></pre>
  
  <p>FWIW this is the call creating the adapter so you can see the parameters I used:</p>
  
  <pre><code>setListAdapter(new CustomArrayAdapter(getActivity(),
                  android.R.layout.simple_list_item_activated_1, android.R.id.text1,
                  KnowUrStuffApp.getDbHelper().getChecklists()));
  </code></pre>
  
  <p>So that code left me where I am now.  However I attempted to force it to color in a couple of redundant ways.  I created a routine called setSelectionHilight that explicitly colors the view background to holo orange.  I then created a onItemSelectedListener assigned to the ListView that called setSelectionHilight.  Then I flat out called the routine explicitly once everything loads.  None of these had any effect:</p>
  
  <pre><code>private void setSelectionHilight(ListView listView, View selectedView) {
      for (int i = 0; i &lt; listView.getChildCount(); i++) {
          View child = listView.getChildAt(i);
          if (child == selectedView)
              child.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_orange_light));
          //else
              //child.setBackgroundResource(0);
      }
  }
  
  
  private OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {
  
  
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          setSelectionHilight((ListView) parent, view);
  
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; parent) {
          setSelectionHilight((ListView) parent, null);
  
      }
  
  };
  </code></pre>
  
  <p>Again, I was just experimenting with the chunk directly above.  You can safely ignore that in considering a solution to my problem, but I include it for a complete picture.  Thanks for any help on solving this.  Another bit of ancillary information is that the ListView is encapsulated in a ListFragment and I am using the default layout that comes baked in with that.  I don't have an explicit XML element for the ListView.</p>