Saturday, 15 October 2016

MultiAutoCompleteTextView example in android studio

Do the following in sequence:-
Step 1:- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:gravity="center_vertical">
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Select City:-"
       android:textSize="30sp"
       android:textColor="@android:color/black"/>

   <MultiAutoCompleteTextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/multilist"
       android:layout_marginTop="16dp"
       android:singleLine="true"/>
   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Submit"
       android:layout_marginTop="30dp"
       android:onClick="submit"/>
</LinearLayout>

Step 2:- MainActivity.java
package supriya.com.multiautocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    MultiAutoCompleteTextView completeTextView;
    String[] list={"Hyderabad","Delhi","Goa","Patna","Telangana","Jaipur"};


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

        completeTextView=(MultiAutoCompleteTextView)findViewById(R.id.multilist);
        completeTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),R.layout.multitextview_design,list);
        completeTextView.setThreshold(1);
        completeTextView.setAdapter(adp);
       completeTextView.setDuplicateParentStateEnabled(false);

    }
    public void submit(View v){
        completeTextView.setText("");
        Toast.makeText(getApplicationContext(),"Submitted",Toast.LENGTH_LONG).show();
    }
}
Step 2:- multitextview_design.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#000000"
    android:text=""
    android:layout_margin="5dp"/>

Step 3:-Output


No comments:

Post a Comment