Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

Am developing AutoComplete functionality in Android Application using AutoCompleteTextView. It works properly and am able to get the list that is assigned based on the keypress.

I face following two issue in AutoCompleteTextView.

1. The Value I enter in Textbox is not visible. say "John" if I type "J" the value is retrieved but am not able to view the value entered into thatAutoCompleteTextView textcontrol

2. In case I select a value from the list, the selected value is not shown/displayed to the textbox.

Can you please help me in this issue. Thanks in Advance.

XML
<pre lang="XML">
// activity_main.xml
<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="com.techmail.progressreport.MainActivity">
     <textview android:id="@+id/lblSubjects" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lblSubjects" android:textappearance="?android:attr/textAppearanceMedium"></textview>
     <autocompletetextview android:id="@+id/txtautocompleteSubjects" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lblSubjects" android:inputtype="textAutoComplete|textAutoCorrect" android:layout_margintop="40dp" android:ems="12"></autocompletetextview>
</relativelayout>
</pre>
//MainActivity.java
<pre lang="Java" >
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] chapters = getResources().getStringArray(R.array.subjectsList);
        // In the onCreate method
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtautocompleteSubjects);
        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,
                android.R.layout.simple_list_item_1,
                chapters);
        textView.setAdapter(adapter);
        textView.setThreshold(1);
    }
</pre>
//string
<pre lang="XML">
<!--?xml version="1.0" encoding="utf-8"?-->
<resources>

    <string name="app_name">ProgressReport</string>
    <string name="hello_world">Hello world!</string>
    <string name="lblSubjects">Subjects</string>
    <string name="action_About">About</string>
<string-array name="subjectsList">
        <item>Tamil</item>
        <item>English</item>
        <item>Maths</item>
        <item>Science</item>
        <item>Social</item>
</string-array>
</resources>
</pre>




Regards,
Mahe
Posted
Updated 3-Oct-14 20:07pm
v2
Comments
Richard MacCutchan 2-Oct-14 3:49am    
You need to show us some of your code if you want help. We cannot guees what your program is doing.
Mahendran Murugesan 4-Oct-14 2:00am    
Hi,

Added the code in question itself. Could you please guide me to display the typed letters in Autocompletetextview
Regards,
Mahe

1 solution

There is nothing wrong with your code, apart from the misuse of lower case letters in a number of class and attribute names. Once I corrected those errors the code worked perfectly.
XML
// activity_main.xml
<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="com.techmail.progressreport.MainActivity">
     <textview android:id="@+id/lblSubjects">
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/lblSubjects"
         android:textAppearance="?android:attr/textAppearanceMedium">
     </textview>
     <autocompletetextview android:id="@+id/txtautocompleteSubjects">
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:inputType="textAutoComplete|textAutoCorrect"
         android:layout_marginTop="40dp"
         android:ems="12">
     </autocompletetextview>
</relativelayout>


Java
// MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        String[] chapters = getResources().getStringArray(R.array.subjectsList);
        // In the onCreate method
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtautocompleteSubjects);
        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,
                android.R.layout.simple_list_item_1,
                chapters);
        textView.setAdapter(adapter);
        textView.setThreshold(1);
    }
</string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900