I'm using the AutoCompleteTextView component to filter member data. I've set it up in my layout file as follows
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/autocomplete_swap_pool"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_height="#dimen/nfl_my_listings_and_my_bids_spinner_height"
android:layout_toLeftOf="#id/icon_on"
android:dropDownSelector="#color/black"
android:dropDownVerticalOffset="5dp"
android:dropDownWidth="wrap_content"
android:inputType="textAutoComplete|textAutoCorrect"
android:popupBackground="#color/white"
android:ems="10"
android:text="" />
I've then initialised it in my code like this
autoTextView = (AutoCompleteTextView) EngineGlobals.iRootActivity.findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineGlobals.iApplicationContext, android.R.layout.simple_dropdown_item_1line, Cards);
autoTextView.setThreshold(1);
autoTextView.setAdapter(adapter);
When I enter text the dropdown box appears but the items are not visible, they are there because if I click on the dropdown box items appear. It looks as if they are white font on a white background.
What am I doing wrong?
I think you need to apply textColor to your hint. For that you need custom row layout for your AutoCompleteTextView.
hint_item.xml
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#android:color/holo_green_dark" />
YourActivity.java
AutoCompleteTextView autoTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> autoadapter = new ArrayAdapter<String>(this, R.layout.hint_item, new String[]{"One", "Two", "Three"});
autoTextView.setAdapter(autoadapter);
Output
Related
I'm trying to create a dynamic spinner (combobox), but I couldn't make it work, it is showing the first item in the spinner, but when I click it, the list with the other items is not showing and nothing is happening.
My activity xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/global_languages"/>
<Spinner
android:id="#+id/localesSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#color/facebookBlue"
android:clickable="true"/>
</LinearLayout>
I'm trying to set items this way:
private void loadLocalesSuccess(Collection<String> locales){
Spinner localesSpinner = (Spinner) findViewById(R.id.localesSpinner);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new ArrayList(locales));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localesSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
what could be the problem?
This code snippet should work, check if the data is proper.
ArrayList<String> locales = new ArrayList<>();
locales.add("English");
locales.add("French");
Spinner localesSpinner = findViewById(R.id.localesSpinner);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, locales);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localesSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
I am developing an Android application and I've never worked with MultiAutoCompleteTextView before.
In my layout.xml I've added:
<MultiAutoCompleteTextView
android:id="#+id/autocomplete_subtest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:ems="10"
android:text="" />
And in my DialogFragment:
try {
subtestAutoCompleteTextView = (MultiAutoCompleteTextView) view.findViewById(R.id.autocomplete_subtest);
ArrayAdapter<String> subtestAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, subtests);
subtestAutoCompleteTextView.setAdapter(subtestAdapter);
subtestAutoCompleteTextView.setThreshold(1);
subtestAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
} catch (Exception e) {
e.printStackTrace();
}
The items, however, appear with a transparent text:
If I click on the list item, with the empty text, it completes my MultiAutoCompleteTextView with an expected option.
Any idea why the text does not show on the item?
You should change the text color of list item. It`s may be white so change to black.
EDIT 1
Just to explain how to solve this, I followed this link: How to change text color of simple list item
Basically, create a custom layout.xml file with the TextView:
<?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:layout_width="fill_parent"
android:background="#drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
And in my code I changed:
ArrayAdapter<String> subtestAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.my_layout_file, subtests);
Change The ArrayAdapter, it will work 100%. I test it myself for you. use this
ArrayAdapter subtestAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,subtests);
And subtests object to String Array. Like this
String [] subtests = {"Enamul","Ashraful", "Tonu", "Shawan","Morshed"};
If you initialize it from database or other place and use ArrayList<String> subtests like this. Then you can assign it to an String Array from ArrayListusing 3 line of code like this
String [] subtests2 = new String[subtests.size()];
for (int i = 0; i < subtests.size(); i++) {
subtests2[i]=subtests.get(i).toString();
}
ArrayAdapter subtestAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,subtests2);
Good Luck...
<MultiAutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:ellipsize="none"
android:ems="10"
android:inputType="textCapWords"
android:scrollHorizontally="true"
android:scrollbars="none"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:background="#null"
/>
Try this above code in your xml file.its working in my case.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List Type"
android:textSize="18sp"
android:padding="7dp"
android:clickable="true"
android:onClick="open_spinner"/>
string.xml
<string-array name="type">
<item>Male</item>
<item>Female</item>
</string-array>
In xml file I already know to make TextView can click. But how I open like spinner when click on TextView?
Declare a spinner with visibility gone in your xml
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:prompt="#string/spinner_title"
/>
inside your open_spinner method :
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, youarrayofStrings);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
I am trying to use two spinners with a custom dropdown yet, only the bottom one is showing with the custom layout when pulled down. I have been trying on my own to figure out why but I cannot.
public class setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setuplayout);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.numberPlayers, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner02);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.gameDifficulty, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
}
}
This is the xml code for spinner_item mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#F9B12F"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:padding="5dip"
android:popupBackground="#000000"
android:background="#000000"
/>
This is the setuplayout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="#+id/mainSetupImage"
android:src="#drawable/setup"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<ImageView
android:id="#+id/players"
android:clickable="true"
android:src="#drawable/players"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
</ImageView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner01"
android:textColor="#F9B12F"
android:layout_toRightOf="#+id/players"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="#drawable/spinner"/>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner02"
android:textColor="#F9B12F"
android:layout_below="#+id/Spinner01"
android:layout_toRightOf="#+id/difficulty"
android:layout_marginTop="25dp"
android:background="#drawable/spinner"/>
<ImageView
android:id="#+id/difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/Spinner02"
android:clickable="true"
android:src="#drawable/difficulty" />
</RelativeLayout>
in 2nd spinner u used adapter rather than adapter2. so use adapter2 and run your code hope it will run. :)
I figured it out with your help. I had used the code "adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)" which was overriding my custom layout. Thanks for highlighting where the two codes were different. That was a huge help. I deleted the quoted lines and it now works.
I'd like to share a little code that will make all people's code much cleaner when making ArrayAdapters.
Just define a static method in a class of your preference (even MainActivity...) to give you the Array adapter without dirting so much your code:
public static ArrayAdapter<CharSequence>
getArrayAdapter( Context c, int arrayId, int idLayout1, int idLayout2){
ArrayAdapter<CharSequence> aa;
aa = ArrayAdapter.createFromResource( c, arrayId , idLayout1);
aa.setDropDownViewResource( idLayout2);
return aa;
}
Then in your code you just need to do it:
myspinner.addAdapter(
this, ThatClass.getArrayAdapter(
R.array.myid, R.layout.id1, R.layout.id2 );
myspinner.setOnItemSelectedListener(this);
/* Remember that the default Android Spinner layout can be get passing these specific layout ids:
android.R.layout.simple_spinner_item as layout1
android.R.layout.simple_spinner_dropdown_item as layout2
*/
I have some problems with the spinner. Depending of my dates, I must add to a TableRow a TextView with an EditText or a Spinner. My array that must be display in Spinner is a little long. I tested my code with an array with short texts, and it looks like this :
Here the single problem is that spinner is not fill_parent.
If I put my array to spinner it looks like this :
In this case, the spinner doesn't look like a spinner and the EditText is not visible any more. When I choose the spinner, it appears this view :
Here I need to display all the text of the array.
This is my code :
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT , TableRow.LayoutParams.WRAP_CONTENT);
tablerow_product[i] = new TableRow(viewToLoad.getContext());
tablerow_product[i].setLayoutParams(lp);
product_spinner[i] = new Spinner(viewToLoad.getContext());
product_spinner[i].setLayoutParams(lp); product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
String[] proba={"red","blue"}; //first image is with this test array
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(), com.Orange.R.layout.my_spinner_textview,spinnerArray); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
product_spinner[i].setAdapter(spinnerArrayAdapter);
tablerow_product[i].addView(product_spinner[i]); Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
and my_spinner_textview.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/textorange_selected"
android:gravity="left"
android:singleLine="false"
android:ellipsize="end"/>
Can anyone help me to solve it? Any idea is welcome. Thanks in advance.
For my problem I found this solution :
Spinner language = (Spinner) findViewById(com.Orange.R.id.current_language_text);
ArrayAdapter adapter = new ArrayAdapter(this,
com.Orange.R.layout.my_spinner_textview, languages);
adapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);
language.setAdapter(adapter);
where languages is a String[] and my_spinner_textview.xml is :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/textorange_selected"
android:paddingLeft="5dp"
android:singleLine="true"
android:ellipsize="end"
/>
simply I did with custom text view like:
ArrayAdapter myAdapter = new ArrayAdapter(context, R.layout.text_view, reasonTypesList);
myAdapter.setDropDownViewResource(R.layout.text_view);
mySpinner.setAdapter(myAdapter);
and here is the text_view layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTextView"
style="?android:attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/padding"
android:text="Custom Text with multi lines" />