Friday, 16 September 2016

AutoCompleteTextview example in Android Studio | How to use of AutoCompleteTextview


Required Classes and Layouts
1.MainActivity.java | activity_java.xml
2.list_item_view.xml :- For list item view

Step1:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:gravity="center_vertical">
    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/actv"
        android:hint="--Select--"
        android:ems="20"
        android:singleLine="true"
        android:completionThreshold="1"/>
</LinearLayout>
Step2:-MainActivity.java
package supriya.com.autocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView actv;
    String[] item={"hyderabad","Andhra Pradesh","Amaravati","Arunachal Pradesh","Itanagar","Assam","Dispur","Bihar","Patna","Chhattisgarh","Raipur","West Bengal"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actv=(AutoCompleteTextView)findViewById(R.id.actv);
        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item_view,item);
        actv.setAdapter(adp);

    }
}
Step3:-list_item_view.xml (hint:-go to app->right click on res->layouts->new->Layouts resource file->ok)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="#FF0000"
    android:paddingLeft="16dp"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Output:-



2 comments: