Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

autoAdapter = newAutoAdapter(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

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

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

in Adapter class.

@Overridepublic View getView(int position, View v, ViewGroup parent)
{
    ViewmView= v ;
    if(mView == null){
        LayoutInflatervi= (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }
    TextViewtext= (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.

Solution 2:

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

AutoCompleteTextViewsearchView= ... 
searchView.setDropDownBackgroundDrawable(newColorDrawable(context.getResources().getColor(R.color.colorId)));

Solution 3:

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"?><TextViewxmlns: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(newArrayAdapter<String>(this,
            R.layout.autocomplete_custom, listContainingText););
    

Run. Bingo!

Solution 4:

It is actually not possible to direct change the color of the auto-complete textView by default. Instead you have to implement the custom adapter for such and inflating the view from adapter usually textView. See this question which will help u to solve your problem. So you can set the properties to the textview as you want.. e.g You must have the custom adapter like below

publicclassMyCustomBrandAdapterextendsArrayAdapter<Brand>{


List<Brand> Brandlist;
Context context;
private ArrayList<Brand> itemsAll;
private ArrayList<Brand> suggestions;
publicMyCustomBrandAdapter(Context context, int textViewResourceId, List<Brand> Brandlist) 
{
    
    super(context,  textViewResourceId, Brandlist);
    this.Brandlist =  Brandlist;
    this.itemsAll = (ArrayList<Brand>) ((ArrayList<Brand>) Brandlist).clone();
    this.suggestions = newArrayList<Brand>();
    this.context = context;
}


public View getView(int position, View convertView, ViewGroup parent) 
{
    Viewv= convertView;
    if (v == null) {
        LayoutInflatervi= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.listview, null);

    }
    BrandBrand= Brandlist.get(position);
    if (Brand != null) {
        TextViewtt= (TextView) v.findViewById(R.id.txtName);
        
        if (tt != null) {
            tt.setText(Brandlist.get(position).toString());
        } else {
            System.out.println("Not getting textview..");
        }
    
    }
    return v;
}
 @Overridepublic Filter getFilter() {
        return nameFilter;
    }

    FilternameFilter=newFilter() {
        public String convertResultToString(Object resultValue) {
            Stringstr= ((Brand)(resultValue)).getBrandDescription1(); 
            return str;
        }
        @Overrideprotected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (Brand brand : itemsAll) {
                    if(brand.getBrandDescription1().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        suggestions.add(brand);
                    }
                }
                FilterResultsfilterResults=newFilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                returnnewFilterResults();
            }
        }
        @OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
            ArrayList<Brand> filteredList = (ArrayList<Brand>) results.values;
            List<Brand> getResult=newArrayList<Brand>();
            if(results != null && results.count > 0) {
                clear();
                for (Brand c : filteredList) {
                    getResult.add(c);
                }
                Iterator<Brand> brandIterator=getResult.iterator();
                while(brandIterator.hasNext()){
                    Brand party=brandIterator.next();
                    add(party);
                }
                notifyDataSetChanged();
            }
        }
    };
    
}

and in this the layout R.layout.listview xml is as follows

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:background="#ffffff" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/txtName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"android:textColor="#000000"android:textSize="12dp"/></LinearLayout></LinearLayout>

Here in this xml. I have set background as white (android:background="#ffffff") and I want text as black (android:textColor="#000000") and size as (android:textSize="12dp") as I want it modified.. I think now it will get u clearly.

Post a Comment for "How To Change The Text Color Of Dropdown In An Autocompletetextview?"