Showing posts with label Auto Complete Text. Show all posts
Showing posts with label Auto Complete Text. Show all posts

Sunday, December 03, 2017

Auto Complete Text Demo, Multi Autocomplete Text

Auto Complete Text is used to show suggestions at the time of data entry.
For the multi selection used multiselection autocomplete textview at the place of autocomplete textview.



Steps to use Multi Auto Complete Text Demo in your project.

  1. Crate a project(AutoCompleteTextDemo) in android studio.

  2. Create a class MainActivity.Java and update it-


    package com.pankaj.autocompletetextviewdemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.MultiAutoCompleteTextView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        //Create objects
        List<String> data = new ArrayList<>();
        MultiAutoCompleteTextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Create list to show suggestions
            for (int i = 1; i <= 500; i++) {
                data.add("" + i);
            }
    
            //Create adapter to add suggestions to autocomplete textview
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, data);
    
            //Initialize textview
            textView = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    
            //Add adapter
            textView.setAdapter(adapter);
    
            //set tockenizer to select multiple items
            textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    
        }
    
        //show selected data on toast
        public void showData(View view) {
            Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
        }
    
    }
    
    
    
    
    
    
     
  3. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        tools:context="com.pankaj.autocompletetextviewdemo.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:orientation="vertical">
    
            <MultiAutoCompleteTextView
                android:id="@+id/autoCompleteTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="type numbers" />
    
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:onClick="showData"
                android:text="Show Selected" />
    
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

  4. All the application development process with Multi Selection Autocomplete Text  has completed, Now run the app and look the screen. 










  5. Good bye, Thanks to read this blog.