title: highlight listview item
body: <p>I need to highlight a listview item when i touch it and to stay highlighted. I tried everything I found but nothing worked. Here is my code:</p>
  
  <p>This is the listview:</p>
  
  <pre><code>&lt;ListView
      android:id="@+id/lvUsers"
      android:layout_width="300dp"
      android:layout_height="fill_parent"
      android:layout_alignParentLeft="true"
      android:background="@drawable/border2"
      android:choiceMode="singleChoice"
      android:padding="5dp" &gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>The <code>@drawable/border2</code> is just a border around the listview.</p>
  
  <p>This is the listview item layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layoutUsersRow"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/list_selector_background"
  android:orientation="vertical" &gt;
  
  &lt;TextView
      android:id="@+id/tvAllUsersName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="10dp"
      android:text="User name"
      android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is list_selector_background:</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/blue" /&gt; &lt;!-- focused --&gt;
      &lt;item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /&gt; &lt;!-- focused and pressed--&gt;
      &lt;item android:state_pressed="true" android:drawable="@color/blue" /&gt; &lt;!-- pressed --&gt;
      &lt;item android:drawable="@color/transparent" /&gt; &lt;!-- default --&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>This is the code from the listview adapter</p>
  
  <pre><code>public class UsersAdapter extends BaseAdapter implements OnClickListener {
  
  LoginActivity context;
  private List&lt;String&gt; listOfUsers;
  
  public UsersAdapter(LoginActivity _context, List&lt;String&gt; listOfUsers) {
      context = _context;
      this.listOfUsers = listOfUsers;
  }
  
  public int getCount() {
      return listOfUsers.size();
  }
  
  public Object getItem(int position) {
      return listOfUsers.get(position);
  }
  
  public long getItemId(int position) {
      return position;
  }
  
  public View getView(int position, View convertView, ViewGroup parent) {
      String entry = listOfUsers.get(position);
      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.users_row, null);
      }
  
      TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
      UserName.setText(entry);
  
      LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
      LinLayout.setFocusableInTouchMode(false);
      LinLayout.setFocusable(false);
      LinLayout.setOnClickListener(this);
  
      return convertView;
  }
  
  public void onClick(View v) {
      // get the row the clicked button is in
      LinearLayout row = (LinearLayout) v;
  
      TextView child = (TextView) row.getChildAt(0);
      if (child.getText().toString().equals("Admin")) {
          Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
          i.putExtra("verificationFor", "ADMIN_LOGIN");
          context.startActivity(i);
      } else {
          context.userClicked(child.getText().toString());
  
          // focus the pin field after selecting user name from the list
          if (context.currentList != null) {
              context.etLoginPin.setVisibility(View.VISIBLE);
              context.tvPin.setVisibility(View.VISIBLE);
              context.etLoginPin.requestFocus();
              context.etLoginPin.setText("");
              InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(context.etLoginPin, 0);
          }
      }
  }
  }
  </code></pre>
  
  <p>This is not working. It only highlights the item while it's pressed. As soon as i release it, the highlight is gone.</p>
  
  <p><strong>HERE IS THE SOLUTION</strong></p>
  
  <p>Remove the <code>android:background="@drawable/list_selector_background"</code> line from the listview item layout. The selector is not needed at all. And here is the adapter's code:</p>
  
  <pre><code>public class UsersAdapter extends BaseAdapter {
  
  LoginActivity context;
  private List&lt;String&gt; listOfUsers;
  boolean[] arrBgcolor;
  private int blue = Color.BLUE;
  private int transparent = Color.TRANSPARENT;
  
  public UsersAdapter(LoginActivity _context, List&lt;String&gt; listOfUsers) {
      context = _context;
      this.listOfUsers = listOfUsers;
  
      arrBgcolor = new boolean[listOfUsers.size()];
      resetArrbg();
  
  }
  
  private void resetArrbg() {
      for (int i = 0; i &lt; arrBgcolor.length; i++) {
          arrBgcolor[i] = false;                  
      }
  }
  
  public int getCount() {
      return listOfUsers.size();
  }
  
  public Object getItem(int position) {
      return listOfUsers.get(position);
  }
  
  public long getItemId(int position) {
      return position;
  }
  
  public View getView(final int position, View convertView, ViewGroup parent) {
      String entry = listOfUsers.get(position);
      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.users_row, null);
      }
  
  
      TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
      UserName.setText(entry);
  
      LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
      LinLayout.setFocusableInTouchMode(false);
      LinLayout.setFocusable(false);
      LinLayout.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              handleClick(v, position);
          }
      });
  
      if (arrBgcolor[position])
          LinLayout.setBackgroundColor(blue);
      else 
          LinLayout.setBackgroundColor(transparent);
  
      return convertView;
  }
  
  public void handleClick(View v, int position) {
      LinearLayout row = (LinearLayout) v;
  
      resetArrbg();
      arrBgcolor[position] = true;
      notifyDataSetChanged();
  
      TextView child = (TextView) row.getChildAt(0);
      if (child.getText().toString().equals("Admin")) {
          Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
          i.putExtra("verificationFor", "ADMIN_LOGIN");
          context.startActivity(i);
      } else {
          context.userClicked(child.getText().toString());
  
          // focus the pin field after selecting user name from the list
          if (context.currentList != null) {
              context.etLoginPin.setVisibility(View.VISIBLE);
              context.tvPin.setVisibility(View.VISIBLE);
              context.etLoginPin.requestFocus();
              context.etLoginPin.setText("");
              InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(context.etLoginPin, 0);
          }
      }
  }
  
  }
  </code></pre>