Thursday, 22 January 2015

MultiAutoCompleteTextView Example









Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10">

        <requestFocus />
    </MultiAutoCompleteTextView>

</RelativeLayout>

MainActivity.java
package com.swamys.multiautocompletetextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {

       MultiAutoCompleteTextView multiAutoCompleteTextView;
       String months[] = { "jan", "feb", "march", "aprl", "may", "jun", "july" };

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);

              ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                           getApplicationContext(),
                           android.R.layout.simple_dropdown_item_1line, months);

              multiAutoCompleteTextView.setAdapter(adapter);
              multiAutoCompleteTextView
                           .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

              multiAutoCompleteTextView
                           .setOnItemClickListener(new OnItemClickListener() {

                                  @Override
                                  public void onItemClick(AdapterView<?> adapterView,
                                                View view, int position, long arg3) {
                                         Toast.makeText(
                                                       getApplicationContext(),
                                                       adapterView.getItemAtPosition(position)
                                                                     .toString(), Toast.LENGTH_LONG).show();
                                  }
                           });

       }
}


No comments:

Post a Comment