Saturday, July 28, 2012

Auto complete demo in android | Searching in Edit text in Android


1-manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="auto.complete"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AutoCompleteDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


2- AutoCompleteDemoActivity.java file
package auto.complete;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class AutoCompleteDemoActivity extends Activity implements TextWatcher {
       TextView textViewSelection;
       AutoCompleteTextView autoComplete;
       String[] fruits = { "apple", "banana", "graps", "orrange", "berry" };

       @Override
       public void onCreate(Bundle icicle) {
              super.onCreate(icicle);
              setContentView(R.layout.main);
              textViewSelection = (TextView) findViewById(R.id.selection);
              autoComplete = (AutoCompleteTextView) findViewById(R.id.edit);
              autoComplete.addTextChangedListener(this);
              autoComplete.setAdapter(new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, fruits));
       }

       public void onTextChanged(CharSequence s, int start, int before, int count) {
              textViewSelection.setText(autoComplete.getText());
       }

       public void beforeTextChanged(CharSequence s, int start, int count,
                     int after) {
              // needed for interface, but not used
       }

       @Override
       public void afterTextChanged(Editable s) {
              // TODO Auto-generated method stub
             
       }      }


3- main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> 
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<AutoCompleteTextView android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"/>

</LinearLayout>

1 comment :

  1. This comment has been removed by the author.

    ReplyDelete

Note: Only a member of this blog may post a comment.