title: selecting contact from autocomplete textview
body: <p>i want to select the contact using autocomplete textview for sending sms. I have almost achieved what i want, but for one minute problem as you can see in the image. How can i solve that?</p>
  
  <p><strong>activity_contact_with_auto.xml</strong></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="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" &gt;
  
  &lt;AutoCompleteTextView
      android:id="@+id/mmWhoNo"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:hint="To...." &gt;
  &lt;/AutoCompleteTextView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>custcontview.xml</strong></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:orientation="vertical" &gt;
  
  &lt;TextView
      android:id="@+id/ccontName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Large Text"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textColor="#A5000000" /&gt;
  
  &lt;TextView
      android:id="@+id/ccontNo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@id/ccontName"
      android:text="Medium Text"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textColor="#A5000000" /&gt;
  
  &lt;TextView
      android:id="@+id/ccontType"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@id/ccontNo"
      android:layout_alignParentRight="true"
      android:layout_marginRight="14dp"
      android:text="Small Text"
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:textColor="#A5000000" /&gt;
   &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>public class ContactWithAuto extends Activity {
  
  private ArrayList&lt;Map&lt;String, String&gt;&gt; mPeopleList;
  private SimpleAdapter mAdapter;
  private AutoCompleteTextView mTxtPhoneNo;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_contact_with_auto);
      mPeopleList = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
      PopulatePeopleList();
      mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
      mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
              new String[] { "Name", "Phone", "Type" }, new int[] {
                      R.id.ccontName, R.id.ccontNo, R.id.ccontType });
      mTxtPhoneNo.setAdapter(mAdapter);
  
  }
  
  public void PopulatePeopleList() {
      mPeopleList.clear();
      Cursor people = getContentResolver().query(
              ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
      while (people.moveToNext()) {
          String contactName = people.getString(people
                  .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          String contactId = people.getString(people
                  .getColumnIndex(ContactsContract.Contacts._ID));
          String hasPhone = people
                  .getString(people
                          .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  
          if ((Integer.parseInt(hasPhone) &gt; 0)){
              // You know have the number so now query it like this
              Cursor phones = getContentResolver().query(
              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
              null,
              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
              null, null);
              while (phones.moveToNext()){
                  //store numbers and display a dialog letting the user select which.
                  String phoneNumber = phones.getString(
                  phones.getColumnIndex(
                  ContactsContract.CommonDataKinds.Phone.NUMBER));
                  String numberType = phones.getString(phones.getColumnIndex(
                  ContactsContract.CommonDataKinds.Phone.TYPE));
                  Map&lt;String, String&gt; NamePhoneType = new HashMap&lt;String, String&gt;();
                  NamePhoneType.put("Name", contactName);
                  NamePhoneType.put("Phone", phoneNumber);
                  if(numberType.equals("0"))
                      NamePhoneType.put("Type", "Work");
                      else
                      if(numberType.equals("1"))
                      NamePhoneType.put("Type", "Home");
                      else if(numberType.equals("2"))
                      NamePhoneType.put("Type",  "Mobile");
                      else
                      NamePhoneType.put("Type", "Other");
                      //Then add this map to the list.
                      mPeopleList.add(NamePhoneType);
              }
              phones.close();
          }
      }
      people.close();
      startManagingCursor(people);
  }
  
  public void onItemClick(AdapterView&lt;?&gt; av, View v, int index, long arg){
      Map&lt;String, String&gt; map = (Map&lt;String, String&gt;) av.getItemAtPosition(index);
      Iterator&lt;String&gt; myVeryOwnIterator = map.keySet().iterator();
      while(myVeryOwnIterator.hasNext()) {
          String key=(String)myVeryOwnIterator.next();
          String value=(String)map.get(key);
          mTxtPhoneNo.setText(value);
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_contact_with_auto, menu);
      return true;
  }
  }
  </code></pre>
  
  <p><strong>Image</strong></p>
  
  <p><img src="https://i.stack.imgur.com/GeB8R.png" alt="screenshot"></p>