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

前端 未结 4 1887
难免孤独
难免孤独 2021-01-05 12:32

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 back

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 13:03

    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

    public class MyCustomBrandAdapter extends ArrayAdapter{
    
    
    List Brandlist;
    Context context;
    private ArrayList itemsAll;
    private ArrayList suggestions;
    public MyCustomBrandAdapter(Context context, int textViewResourceId, List Brandlist) 
    {
        
        super(context,  textViewResourceId, Brandlist);
        this.Brandlist =  Brandlist;
        this.itemsAll = (ArrayList) ((ArrayList) Brandlist).clone();
        this.suggestions = new ArrayList();
        this.context = context;
    }
    
    
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listview, null);
    
        }
        Brand Brand = Brandlist.get(position);
        if (Brand != null) {
            TextView tt = (TextView) v.findViewById(R.id.txtName);
            
            if (tt != null) {
                tt.setText(Brandlist.get(position).toString());
            } else {
                System.out.println("Not getting textview..");
            }
        
        }
        return v;
    }
     @Override
        public Filter getFilter() {
            return nameFilter;
        }
    
        Filter nameFilter = new Filter() {
            public String convertResultToString(Object resultValue) {
                String str = ((Brand)(resultValue)).getBrandDescription1(); 
                return str;
            }
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                if(constraint != null) {
                    suggestions.clear();
                    for (Brand brand : itemsAll) {
                        if(brand.getBrandDescription1().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                            suggestions.add(brand);
                        }
                    }
                    FilterResults filterResults = new FilterResults();
                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                    return filterResults;
                } else {
                    return new FilterResults();
                }
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                ArrayList filteredList = (ArrayList) results.values;
                List getResult=new ArrayList();
                if(results != null && results.count > 0) {
                    clear();
                    for (Brand c : filteredList) {
                        getResult.add(c);
                    }
                    Iterator 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

        
    
    
        
    
            
    
        
    
    
    

    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.

提交回复
热议问题