I have made an AutoCompletetextView. The items in the dropdown of AutoCompleteTextView are not visible. How to change the color of of those items.
This is how it looks:
I have made an AutoCompletetextView. The items in the dropdown of AutoCompleteTextView are not visible. How to change the color of of those items.
This is how it looks:
For controlling the way you display items in your autocomplete view, you have to set the textViewResourceId in your adapter. You can use the ArrayAdapter and give android.R.layout.simple_dropdown_item_1line
as the textViewResourceId as shown below.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, yourList);
AutoCompleteTextView autocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_box);
autocompleteView.setAdapter(adapter);
OR
if you want to create your own style for the items displayed, please create an XML with TextView
as the root element like this (lets name it my_custom_dropdown.xml
with black color text and white background)
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="5sp"
android:textColor="@color/black"
android:background="@color/white"/>
Then refer to the xml in your adapter as below -
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_dropdown, yourList);
If changing code from "android.R.layout.simple_list_item_1"
to "android.R.layout.simple_dropdown_item_1line"
didn't work for you,
you should try to write this code before setContentView
setTheme(android.R.style.Theme);
It worked for me :)
Just use "android.R.layout.simple_list_item_1"
instead of "android.R.layout.simple_dropdown_item_1line"
.....your problem will be solved...:)
I create a example project: AutoCompleteTextViewAdapter(Github Repo)
You need to implement follow line of source code:
Activity
ArrayAdapter<String> myCustomAdapter = new ArrayAdapter<String>(this, R.layout.text_custom_view, countriesNames);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view);
textView.setAdapter(myCustomAdapter);
XML with Custom TextView
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/country_name"
style="@style/CustomTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
Final Result
Here is answer in hope others will benefit
public class SelectState extends Activity {
final static int[] to = new int[] { android.R.id.text1 };
final static String[] from = new String[] { "state" };
private TextView mStateCapitalView;
private AutoCompleteTextView mStateNameView;
private AutoCompleteDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new AutoCompleteDbAdapter(this);
setContentView(R.layout.selectstate);
Button confirmButton = (Button) findViewById(R.id.confirm);
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
mStateCapitalView = (TextView) findViewById(R.id.state_capital);
mStateNameView = (AutoCompleteTextView) findViewById(R.id.state_name);
// Create a SimpleCursorAdapter for the State Name field.
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,
android.R.layout.simple_dropdown_item_1line, null,
from, to);
mStateNameView.setAdapter(adapter);
// Set an OnItemClickListener, to update dependent fields when
// a choice is made in the AutoCompleteTextView.
mStateNameView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the
// result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String capital =
cursor.getString(cursor.getColumnIndexOrThrow("capital"));
// Update the parent class's TextView
mStateCapitalView.setText(capital);
}
});
// Set the CursorToStringConverter, to provide the labels for the
// choices to be displayed in the AutoCompleteTextView.
adapter.setCursorToStringConverter(new CursorToStringConverter() {
public String convertToString(android.database.Cursor cursor) {
// Get the label for this row out of the "state" column
final int columnIndex = cursor.getColumnIndexOrThrow("state");
final String str = cursor.getString(columnIndex);
return str;
}
});
// Set the FilterQueryProvider, to run queries for choices
// that match the specified input.
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// Search for states whose names begin with the specified letters.
Cursor cursor = mDbHelper.getMatchingStates(
(constraint != null ? constraint.toString() : null));
return cursor;
}
});
}
}
Please check line final static int[] to = new int[] { android.R.id.text1 };
Here is the simplest way. Create you own TextView xml file. For example custom_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/font_content"
android:padding="5sp"
android:textSize="16sp"
android:layout_width="match_parent"
android:background="@drawable/any_background"
android:singleLine="true"
android:gravity="center"
android:layout_height="match_parent"/>
then in your activity
//Create adapter object
ArrayAdapter<String> adpt = new ArrayAdapter<String>(context,R.layout.mytextview, what_ever_array_object_here);
searchAutoComplete.setAdapter(adpt);