Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text color of dropdown in an AutoCompleteTextView?

How can I change the text color in dropdown menu of an AutoCompleteTextView? Or how can I change the background color of a dropdown, by default dropdown's background color is white.

like image 646
Pavan Anadkat Avatar asked Oct 13 '12 20:10

Pavan Anadkat


3 Answers

you need to create custom adapter class for this, then you can set textview to this like.

autoAdapter = new AutoAdapter(this, R.layout.autocompletelistview_test1,
            edittext);    

Here is autocompletelistview_test1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dip"
android:gravity="center_vertical"

android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="12dip"
android:textStyle="bold"
android:textColor="#cc3333"
android:id="@+id/textview"
android:textSize="14sp" />

use following code to change background color of dropdown like

 autocomplete.setDropDownBackgroundResource(R.color.autocompletet_background_color);  

and you can set value like this in string.xml

   <color name="autocompletet_background_color">#EFEFEF</color>    

If you want to change text color of dropdown item then do like following.

in Adapter class.

 @Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }
    TextView text = (TextView) mView.findViewById(R.id.textview);        
    if(getItem(position) != null )
    {
        text.setTypeface(faceBold);
        if(getItem(position).equals("check some condition if you want. "))
        {
            text.setTextColor(Color.parseColor("#999999"));
            text.setText("set some text");               
        }           
        else
        {
            text.setTextColor(Color.parseColor("#cc3333"));
                text.setText(getItem(position));
        }           
    }
    return mView;
}    

Let me know if you have any question.

like image 174
Sandip Armal Patil Avatar answered Oct 22 '22 10:10

Sandip Armal Patil


While it might be tricky to change the text color. It is easy to change the background color of the drop down menu

AutoCompleteTextView searchView = ... 
searchView.setDropDownBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.colorId)));
like image 36
Ilya Gazman Avatar answered Oct 22 '22 09:10

Ilya Gazman


I solved this without inflating the layout using a custom adapter

  1. Simply create a layout autocomplete_custom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/any_id"
    android:textColor="#ffffff"/>
    
  2. Use it in your adapter

    autocomplete.setAdapter(new ArrayAdapter<String>(this,
            R.layout.autocomplete_custom, listContainingText););
    

Run. Bingo!

like image 1
reverie_ss Avatar answered Oct 22 '22 09:10

reverie_ss