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.
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.
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)));
I solved this without inflating the layout using a custom adapter
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"/>
Use it in your adapter
autocomplete.setAdapter(new ArrayAdapter<String>(this,
R.layout.autocomplete_custom, listContainingText););
Run. Bingo!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With