Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView background/foreground color

Tags:

java

android

xml

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);
like image 746
AHméd Net Avatar asked Aug 02 '12 23:08

AHméd Net


2 Answers

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);
like image 120
xWAV92OdD445CbzcZAEyunk2M06fJi Avatar answered Oct 12 '22 22:10

xWAV92OdD445CbzcZAEyunk2M06fJi


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!

like image 21
Sam Avatar answered Oct 12 '22 22:10

Sam