Monday, 21 November 2016

AutoComepleteTextView example in android?

AutoCompleteTextView:
          Android AutoCompleteTextView is an editable text field, it displays a list of suggestions in a drop down menu automatically while the user is entering the characters from which user can select only one suggestion.
              To use AutoCompleteTextView you have to first create an AutoCompleteTextView field in the xml file. its syntax is given below.

<AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:id="@+id/actv"
        android:completionThreshold="1"
/>

After this now get the reference of AutoComepleteTextView as below.
AutoCompleteTextView actv=(AutoCompleteTextView)findViewById(R.id.actv);

The next thing you need to do is to specify the list of suggestions items to be displayed. You can specify the list items as an ArrayList in java. Syntax is given below.

final ArrayList<String> list=new ArrayList<String>();
  list.clear();
  list.add("India");
  list.add("Pakistan");
  list.add("china");
  list.add("US");
 
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,list);
actv.setAdapter(adp);

The array adapter class is responsible for displaying the data as list in the suggestion box of the text field. The setAdapter method is used to set the adapter of the autoCompleteTextView. Apart from these methods, the other methods of Auto Complete are listed below.        

Sr.No
Method & description
1
getAdapter()
This method returns a filterable list adapter used for auto completion
2
getCompletionHint()
This method returns optional hint text displayed at the bottom of the the matching list
3
getDropDownAnchor()
This method returns returns the id for the view that the auto-complete drop down list is anchored to.
4
getListSelection()
This method returns the position of the dropdown view selection, if there is one
5
isPopupShowing()
This method indicates whether the popup menu is showing
6
setText(CharSequence text, boolean filter)
This method sets text except that it can disable filtering
7
showDropDown()
This method displays the drop down on screen.


Android AutoCompleteTextView Example:

Step1)  In order to use AutoCompleteTextView you have to first create an AutoCompletTextView Field in the xml. Its syntax is given below. Create activity.xml file in layout folder and write the below code.
activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"  android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"  tools:context="home.actvtest.MainActivity">  

    <AutoCompleteTextView    
         android:layout_width="match_parent" 
         android:layout_height="70dp"    
         android:id="@+id/actv"     
         android:completionThreshold="1"/>

</RelativeLayout>


Step 2) Create MainActivity.java in java folder and write the below code.
MainActivity.java

package home.actvtest;
import
android.app.Activity;
import
android.os.Bundle;
import
android.widget.ArrayAdapter;
import
android.widget.AutoCompleteTextView;
import
java.util.ArrayList;

public class
MainActivity extends Activity
{
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);


       
AutoCompleteTextView actv=(AutoCompleteTextView)findViewById(R.id.actv);

        final
ArrayList<String> list=new ArrayList<String>();
       
list.clear();

       
list.add("India");
       
list.add("Pakistan");
       
list.add("china");
       
list.add("US");
       
list.add("Banglaedesh");
       
list.add("Barma");
       
list.add("Rashya");
       
list.add("Iran");
       
list.add("Iraq");
       
list.add("Itali");
       
list.add("Brezil");
       
list.add("Srilanka");

       
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,list);

       
actv.setAdapter(adp);

   
}
}
 
 
Step3) write the below code in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="home.actvtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>



Output:










No comments:

Post a Comment