11

I have made an AutoCompletetextView. The items in the dropdown of AutoCompleteTextView are not visible. How to change the color of of those items.

This is how it looks:

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vipul J
  • 6,641
  • 9
  • 46
  • 61
  • How do you know that you are getting any auto-completion results, maybe there is nothing to show? Have you tried to LOG the results and know that there actually exists some results? – theAlse Jul 11 '12 at 07:21
  • 1
    Yes, the results are there. I have checked that. Infact if I hold click on any item, it showed up. – Vipul J Jul 11 '12 at 08:17
  • 1
    did you update to adt r20? If you do, you'll change theme in Manifest to Black theme. I don't know why all new created layout has change to theme.Light so text color is white – Trung Nguyen Jul 19 '12 at 10:01

7 Answers7

14

For controlling the way you display items in your autocomplete view, you have to set the textViewResourceId in your adapter. You can use the ArrayAdapter and give android.R.layout.simple_dropdown_item_1line as the textViewResourceId as shown below.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, yourList);
AutoCompleteTextView autocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_box);
autocompleteView.setAdapter(adapter);

OR

if you want to create your own style for the items displayed, please create an XML with TextView as the root element like this (lets name it my_custom_dropdown.xml with black color text and white background)

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:padding="5sp"
    android:textColor="@color/black"
    android:background="@color/white"/>

Then refer to the xml in your adapter as below -

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_dropdown, yourList);
Mukul Jain
  • 1,807
  • 9
  • 26
  • 38
7

Just to point out that by using android.R.layout.simple_dropdown_item_1line it will give you the same issue you encountered above. So you're better off just creating your own TextView in an .xml file.

Harmlezz
  • 7,972
  • 27
  • 35
janex
  • 497
  • 4
  • 14
5

If changing code from "android.R.layout.simple_list_item_1" to "android.R.layout.simple_dropdown_item_1line" didn't work for you,

you should try to write this code before setContentView

setTheme(android.R.style.Theme);

It worked for me :)

Emre Koç
  • 1,343
  • 1
  • 25
  • 44
  • Did you specify the theme in your manifest? This work's as well and you can apply it for the whole app, or specific activities. – Sambuxc May 22 '14 at 10:25
0

Just use "android.R.layout.simple_list_item_1" instead of "android.R.layout.simple_dropdown_item_1line".....your problem will be solved...:)

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36
0

I create a example project: AutoCompleteTextViewAdapter(Github Repo)

You need to implement follow line of source code:

Activity

ArrayAdapter<String> myCustomAdapter = new ArrayAdapter<String>(this, R.layout.text_custom_view, countriesNames);

final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view);
textView.setAdapter(myCustomAdapter);

XML with Custom TextView

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/country_name"
    style="@style/CustomTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" />

Final Result

AutoCompleteTextViewAdapter

d.danailov
  • 9,594
  • 4
  • 51
  • 36
0

Here is answer in hope others will benefit

http://www.outofwhatbox.com/blog/2010/11/android-simpler-autocompletetextview-with-simplecursoradapter/

public class SelectState extends Activity {

final static int[] to = new int[] { android.R.id.text1 };  

    final static String[] from = new String[] { "state" };

    private TextView mStateCapitalView;
    private AutoCompleteTextView mStateNameView;
    private AutoCompleteDbAdapter mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new AutoCompleteDbAdapter(this);
        setContentView(R.layout.selectstate);
        Button confirmButton = (Button) findViewById(R.id.confirm);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });

        mStateCapitalView = (TextView) findViewById(R.id.state_capital);
        mStateNameView = (AutoCompleteTextView) findViewById(R.id.state_name);

        // Create a SimpleCursorAdapter for the State Name field.
        SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, 
                    android.R.layout.simple_dropdown_item_1line, null,
                    from, to);
        mStateNameView.setAdapter(adapter);

        // Set an OnItemClickListener, to update dependent fields when
        // a choice is made in the AutoCompleteTextView.
        mStateNameView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view,
                        int position, long id) {
                // Get the cursor, positioned to the corresponding row in the
                // result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String capital = 
                    cursor.getString(cursor.getColumnIndexOrThrow("capital"));

                // Update the parent class's TextView
                mStateCapitalView.setText(capital);
            }
        });

        // Set the CursorToStringConverter, to provide the labels for the
        // choices to be displayed in the AutoCompleteTextView.
        adapter.setCursorToStringConverter(new CursorToStringConverter() {
            public String convertToString(android.database.Cursor cursor) {
                // Get the label for this row out of the "state" column
                final int columnIndex = cursor.getColumnIndexOrThrow("state");
                final String str = cursor.getString(columnIndex);
                return str;
            }
        });

        // Set the FilterQueryProvider, to run queries for choices
        // that match the specified input.
        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                // Search for states whose names begin with the specified letters.
                Cursor cursor = mDbHelper.getMatchingStates(
                        (constraint != null ? constraint.toString() : null));
                return cursor;
            }
        });
    }
}

Please check line final static int[] to = new int[] { android.R.id.text1 };

Vijay
  • 2,021
  • 4
  • 24
  • 33
0

Here is the simplest way. Create you own TextView xml file. For example custom_textview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tv"
     android:textColor="@color/font_content"
     android:padding="5sp"
     android:textSize="16sp"
     android:layout_width="match_parent"
     android:background="@drawable/any_background"
     android:singleLine="true"
     android:gravity="center"
     android:layout_height="match_parent"/>

then in your activity

    //Create adapter object
    ArrayAdapter<String> adpt = new ArrayAdapter<String>(context,R.layout.mytextview, what_ever_array_object_here);
    searchAutoComplete.setAdapter(adpt);
Yosidroid
  • 2,053
  • 16
  • 15