AutoComplete widget code...

In this android application we will use Auto Complete features.I mean, when user try to start typing a string in a AutoCompleteTextView field,it gives suggesion as per user input.For this we take some strings in our code.When the user input matches with one or more string the strings are shown accordingly.Then the user can type the full text or simply select the desired string from the list.In our code the selected string is shown in a TextView.
For this application we will take a XML layout containing a TextView and a AutoCompleteTextView.The XML code is:

<?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>


Now we write the code to implement the AutoCompleteTextView widget.The strings in the array(items) are the suggestion that will pop up during user input.Now look at the code below:

package com.android.autocomplete;

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 AutoCom extends Activity implements TextWatcher{

   
    TextView selection;
    AutoCompleteTextView edit;
    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
    "consectetuer", "adipiscing", "elit", "morbi", "vel",
    "ligula", "vitae", "arcu", "aliquet", "mollis",
    "etiam", "vel", "erat", "placerat", "ante",
    "porttitor", "sodales", "pellentesque", "augue", "purus"};


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        edit=(AutoCompleteTextView)findViewById(R.id.edit);
       
        edit.addTextChangedListener(this);
        edit.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line,
        items));
            }
    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        selection.setText(edit.getText());
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub
       
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
       
    }
}

Note,in this code TextWatcher class is extended that watch the user input and based on this give suggestions by it's methods.And after selection the selected one is shown in the TextView(named "selection" here).

Hope you will enjoy this....
Happy coding...

No comments:

Post a Comment