title: Change background color of textview on touch
body: <p>I'm having to simulate a list view because I have a very lengthy layout, where I'm using a scrollview. To simulate a list view, I'm using a linear layout having a vertical orientation containing the following layout a number of times</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
          &lt;TextView
              android:id="@+id/txt_text_view_directclass"
              android:layout_width="wrap_content"
              android:padding="10dp"
              android:layout_height="wrap_content" /&gt;
          &lt;View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="@android:color/darker_gray"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I'm wishing to simulate the ontouch event on each of these item to change the background color of each of these item when the user touches it. Also, I need to have click event for each of the item</p>
  
  <p>The code below does not work very fine.</p>
  
  <pre><code>final View rowView = getActivity().getLayoutInflater().inflate(R.layout.text_view_directclass, null);
  
                   rowView.setOnTouchListener(new OnTouchListener() {
  
                      @Override
                      public boolean onTouch(View v, MotionEvent event) {
                          switch(event.getAction()) {
                             case MotionEvent.ACTION_DOWN:
                                  v.setBackgroundColor(Color.BLUE);
                                  break;
                             case MotionEvent.ACTION_UP:
                                  v.setBackgroundColor(Color.BLACK);
                                  break;
                         }
                         return true;
                      }
                  });
  </code></pre>
  
  <p>It correctly toggles the color on action up and action down. But I select a "row" then moves my finger down without leaving the screen and then leave the screen, it doesn't change the color of that row. ANy better solution? </p>
  
  <p>I've tried using a selector based on the first answer. It doesn't work, </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/directsubclass_row_selector"
          android:orientation="vertical" &gt;
          &lt;TextView
              android:id="@+id/txt_text_view_directclass"
              android:layout_width="wrap_content"
              android:padding="10dp"
              android:layout_height="wrap_content" /&gt;
          &lt;View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="@android:color/darker_gray"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Selector code:</strong></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_focused="true" android:drawable="@color/abs__background_holo_light"/&gt;
      &lt;item android:drawable="@color/abs__background_holo_dark"/&gt;
  &lt;/selector&gt;
  </code></pre>