title: Highlight custom listview item when long click
body: <p>I have created a custom <code>listview</code> which has an <code>ImageView</code> on the left and a <code>TextView</code> on the right. And implementing a multi-selection <code>Listview</code> with <code>actionbar</code> context menu. </p>
  
  <p>The problem is, when i long click on an item it didn't gets highlighted. </p>
  
  <p>Here is how i setup the <code>listview</code> in my <code>ListFragment</code>  </p>
  
  <p>PasswordsFragment.java </p>
  
  <pre><code>package mohd.itcs.safewallet;
  
  import android.os.Bundle;
  import android.support.v4.app.ListFragment;
  import android.view.ActionMode;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AbsListView.MultiChoiceModeListener;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemLongClickListener;
  import android.widget.ListView;
  
  public class PasswordsFragment extends ListFragment {
  
  private String titles[] = { "item1", "item2", "item3" };
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
      setListAdapter(new CustomPasswordsList(getActivity(), titles));
  
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
  
      super.onActivityCreated(savedInstanceState);
  
      /*
       * Setup Multiple Selection Mode
       */
      getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
      getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
  
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; listview, View item,
                  int position, long id) {
              getListView().setItemChecked(position, true);
              return true;
          }
  
      });
  
      getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
  
          @Override
          public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
              // TODO Auto-generated method stub
              return false;
          }
  
          /*
           * Inflate Actionbar Menu for Passwords Multiple Selection
           */
          @Override
          public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
              getActivity().getMenuInflater().inflate(
                      R.menu.passwords_context_menu, menu);
              return true;
          }
  
          @Override
          public void onDestroyActionMode(ActionMode arg0) {
          }
  
          @Override
          public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
              return false;
          }
  
          @Override
          public void onItemCheckedStateChanged(ActionMode actionMode,
                  int position, long id, boolean arg3) {
              /*
               * Change Title bar to number of selection
               */
              int checkedItems = getListView().getCheckedItemCount();
              actionMode.setTitle(String.valueOf(checkedItems) + " Selected");
          }
      });
  
  }
  
  }
  </code></pre>
  
  <p>CustomPasswordsList.java </p>
  
  <pre><code>package mohd.itcs.safewallet;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class CustomPasswordsList extends ArrayAdapter&lt;String&gt; {
  
  private final Context context;
  private final String values[];
  
  public CustomPasswordsList(Context context, String[] values)
  {
      super(context, R.layout.password_list_item, values);
      this.context = context;
      this.values = values;
  }
  
  public View getView(int position, View convertView, ViewGroup parent)
  {
      LayoutInflater inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      View rowView = inflater.inflate(R.layout.password_list_item, parent, false);
      TextView passwordTitle = (TextView) rowView.findViewById(R.id.textView_passwordTitle);
      ImageView passwordIcon = (ImageView) rowView.findViewById(R.id.imageView_passwordIcon);
  
      passwordTitle.setText(values[position]);
  
      passwordIcon.setImageResource(R.drawable.facebook);
  
  
      return rowView;
  }
  
  }
  </code></pre>
  
  <p>passwords_list_item </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="match_parent"
      android:weightSum="1"
      android:orientation="horizontal" &gt;
  
  &lt;ImageView 
      android:id="@+id/imageView_passwordIcon"
      android:layout_height="60dp"
      android:contentDescription="@string/imageView_passwordIcon"
      android:layout_width="60dp" /&gt;
  
  &lt;TextView 
      android:id="@+id/textView_passwordTitle"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:paddingLeft="10dp"
      android:paddingTop="15dp"
      android:textSize="16sp"
      android:textColor="#000000"
      android:layout_weight="1" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>