12

I am working on AutoCompleteTextView.Its working fine but the dropdown text is always white text on white background. this picture explain my problem

picture explain my problem

enter image description here

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000"
    >
    <!-- <AutoCompleteTextView  -->
    <AutoCompleteTextView
        android:id="@+id/text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:hint="Tapez votre 1texte"
        android:textColor="#000"        
        />
</LinearLayout>

Java:

view = (AutoCompleteTextView)findViewById(R.id.text);        
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
adapter.setDropDownViewResource(R.drawable.ic_action_search);//dd
view.setAdapter(adapter);
Sam
  • 86,580
  • 20
  • 181
  • 179
AHmedRef
  • 2,555
  • 12
  • 43
  • 75

7 Answers7

32

If you want to change the look of the dropdown items change the XML layout you pass to the ArrayAdapter, (in your case this is android.R.layout.simple_dropdown_item_1line).

Lets make a new layout named my_list_item.xml in res/layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:textAppearance="?android:attr/textAppearanceMediumInverse"
    android:textColor="#00f" />

This text is smaller, blue, and centered. I don't recommend using this layout, it's more to demonstrate that you can customize it.

Now use this instead:

view = (AutoCompleteTextView)findViewById(R.id.text);        
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, data);
view.setAdapter(adapter);

When you set attributes here (like the text color):

<AutoCompleteTextView
    android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:hint="Tapez votre 1texte"
    android:textColor="#000"        
    />

You are only changing the box above the dropdown list (the box that you see before you interact with the AutoCompleteTextView). Hope that helps!

Sam
  • 86,580
  • 20
  • 181
  • 179
  • If you dont want to create custom adapter Just use `android.R.layout.simple_list_item_1" instead of "android.R.layout.simple_dropdown_item_1line` – bCliks Sep 18 '13 at 13:22
11

This question might be old but I believe this is very important for me to share. I had been experiencing the same problem as OP but it is because I was using 'getApplicationContext()' in place of 'this'


Wrong

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


Correct

ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, PLACES);
  • 4
    If the adapter initialization is inside another thread or runnable then instead of using `getApplicationContext()` use `YourActivityName.this` instead. This will bring the text color back to default black from white. – Ghost-Man Dec 10 '14 at 08:38
  • 1
    Thank you so much, This fixed it for me, the answer should be updated to add this alternative. – Feyisayo Sonubi Mar 28 '16 at 12:22
  • Also, when your in Fragment, you can just use `getActivity()`. – Simon Oct 02 '17 at 15:35
0

A tricky solution for this is: Add this line setTheme(android.R.style.Theme); before setContentView() in your activity file in which your autocompletetextview is present. Let me know if this works.

Kanth
  • 6,681
  • 3
  • 29
  • 41
  • 2
    Just use "android.R.layout.simple_list_item_1" instead of "android.R.layout.simple_dropdown_item_1line" n solve proble – Smily Dec 19 '12 at 10:45
0

I tried setting up the theme before setcontext, tried different resources parameter in arrayAdapter and tried different theme ,but nothing helped.

Then I changed the context from 'this' to 'getApplicationContext' but the problem was persistent.

Finally I changed the context parameter to "getBaseContext()" and the problem was solved.

amol khedkar
  • 141
  • 1
  • 3
0

i got my solution in slecting layout of "select_dialog_singlechoice ArrayAdapter<String > s1= new ArrayAdapter<String> (this,android.R.layout.select_dialog_singlechoice,state);enter image description here

instead of get application context write "this" in ArrayAdapter<>;

try using different layouts u will definately solve your queries

0

I solved this issue by doing a simple tricky Way,

Just change the Theme of your activity declared in your AndroidManifest.xml as,

 <activity
   android:name=".YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:windowSoftInputMode="stateHidden"></activity>

and in the Activity as follows,

ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getActivity(), android.R.layout.select_dialog_item, arraylist);
Gowtham. R
  • 698
  • 5
  • 11
-1

Answer by didldum https://code.google.com/p/android/issues/detail?id=5237

workaround by extending the theme and overriding the 2 styles for the typed text and the suggest text:

  1. use an extended theme in your manifest: ... ...

  2. create the new theme (res/values/themes.xml) which uses fixed styles: ... @style/AutoCompleteTextViewLight @style/Widget.DropDownItemLight ...

  3. create the styles (res/values/styles.xml) which fix the color: ... @android:color/primary_text_light @android:color/primary_text_light

gustavomcastro
  • 155
  • 1
  • 9