How to change dropdown text color in AutoCompleteTextView?

How to change text color in AutoCompleteTextView popup menu? Or how can I change the background color of the drop-down list, the default drop-down background color is white.

+5
source share
5 answers

To do this, you need to create your own adapter class, then you can set a text view for it.

 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 the following code to change the background color of the dropdown menu e.g.

  autocomplete.setDropDownBackgroundResource(R.color.autocompletet_background_color); 

and you can set a value like this in string.xml

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

If you want to change the text color of the drop-down element, follow these steps:

in the 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 questions.

+11
source

Although it can be difficult to change the color of the text. Easy change background color of dropdown menu

 AutoCompleteTextView searchView = ... searchView.setDropDownBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.colorId))); 
+4
source

In fact, it is not possible to directly change the default textView auto-fill color. Instead, you should implement a custom adapter for this and inflate the view from the adapter, usually a textView. See this question to help you solve your problem. This way you can set the properties in the text box as you want. for example, you must have a custom adapter as shown below

 public class MyCustomBrandAdapter extends ArrayAdapter<Brand>{ List<Brand> Brandlist; Context context; private ArrayList<Brand> itemsAll; private ArrayList<Brand> suggestions; public MyCustomBrandAdapter(Context context, int textViewResourceId, List<Brand> Brandlist) { super(context, textViewResourceId, Brandlist); this.Brandlist = Brandlist; this.itemsAll = (ArrayList<Brand>) ((ArrayList<Brand>) Brandlist).clone(); this.suggestions = new ArrayList<Brand>(); 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<Brand> filteredList = (ArrayList<Brand>) results.values; List<Brand> getResult=new ArrayList<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 of R.layout.listview xml is as follows

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android: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 set the background to white (android:background="#ffffff") and I want the text to be black (android:textColor="#000000") and the size to (android:textSize="12dp") as I want so that it is changed .. I think now it will be clear.

0
source

I solved it without inflating the layout using the adapter

  1. Just create an autocomplete_custom.xml layout

     <?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 the adapter

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

To run. Bingo!

0
source

I solved this problem by making 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 Activity as follows ,

 ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.select_dialog_item, arraylist); 
0
source

Source: https://habr.com/ru/post/1439577/


All Articles