Just Simple Info

Pages

Search Auto Suggest In Android (AutoCompleteTextView)



AutoCompleteTextView in one of the most important android view for user experience. Imagine your application has ListView with hundred of item display. It’s hard for users to find the item they want to see.

Thanks for AutoCompleteTextView, users will just type the chunk of word of item they want to see the AutoCompleteTextView will just display items based on what user type in.


Below is the simple code on how to implement AutoCompleteTextView:



public class MainActivity extends Activity {

 
   private static final String[] STUDENT = new String[] {"Juan", "Rose", "Lara", "Leila", "Aileen","Maria"};
 
   TextView noteTextView;
   AutoCompleteTextView autoCompleteTextView;
   LinearLayout linearLayout;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.init();
    }
    /**
     * 
     */
    private void init(){
     /**
      * adapter
      */
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, STUDENT);
     
     /**
      * linearLayout
      */
     this.linearLayout = new LinearLayout(this);
     this.linearLayout.setOrientation(LinearLayout.VERTICAL);
     
     /**
      * autoCompleteTextView
      */
     this.autoCompleteTextView = new AutoCompleteTextView(this);
     this.autoCompleteTextView.setHint("Type here");
     this.autoCompleteTextView.setAdapter(adapter);
     
     //Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
     this.autoCompleteTextView.setThreshold(1);
     
     /**
      * noteTextView
      */
     this.noteTextView = new TextView(this);
     this.noteTextView.setText(this.noteText());
     this.noteTextView.setPadding(5, 5, 5, 5);
     
     this.linearLayout.addView(this.autoCompleteTextView);
     this.linearLayout.addView(this.noteTextView);
     
     this.setContentView(this.linearLayout);
     
    }
    /*
     * create note
     */
    private String noteText(){
     String note = "Type The Following Names :\n";
     
     for(int x = 0 ; x < STUDENT.length ; x++){
      
      note += "     "+STUDENT[x]+"\n";
     }
     
     return note;
    }

    
}

Read more about AutoCompleteTextView here.

Download Source Code 

Happy coding...

No comments:

Post a Comment