I have a TextInputLayout with style ExposedDropdownMenu. this TextInputLayout contains an AutoCompleteTextView.
I can add a list item inside this AutoCompleteTextView using an adapter.
mLightSensorGainTextInput = (AutoCompleteTextView) findViewById(R.id.paramLightSensorGain);
String[] gainStrArray = new String[] {"1", "2"};
ArrayAdapter<String> gainAdapter = new ArrayAdapter<>(this, R.layout.version_list_item, gainStrArray);
mLightSensorGainTextInput.setAdapter(gainAdapter);
I would like to set one value displayed when I click a "setDefaultButton" but I have always the initial view:
I tried :
mLightSensorGainTextInput.setListSelection(0);
mLightSensorGainTextInput.setSelection(0);
but none of the 2 methods shows the value expected which is : 1.
Do you have any suggestion?
Best regards
Mich
Please here is more informations about what I expect. Only last operation (default button) does not work.
Try setText():
mLightSensorGainTextInput.setText("xxx");
As your items are text-based, you can get the first item from the adapter by autoCompleteTextView.getAdapter().getItem(position); then to make it the current selection; set the text of the autoCompleteTextView to that item: mLightSensorGainTextInput.setText((mLightSensorGainTextInput.getAdapter().getItem(0)).toString());
The solution is to use the filter:
mLightSensorGainTextInput.setText("1", false);
Related
Case: I have a screen (EditActivity) with a product creating/editing form. This screen has a layout in which there is a drop-down list product category. The list of items in this drop-down is set via ArrayAdapter.createFromResource. I don't have a problem when I use this screen as a creating form. The problem appears when I use this screen as a editing form. I'm trying to set the product category initial value to the field via setText(). It works, and value appears in the dropdown. But other values from the ArrayAdapter disappear and I can't choose them. I also tried to set value using setSelection(), but I'm not sure this is correct way.
Question: How do I insert the initial value into the dropdown so that I can see and select another values from the list?
EditActivity layout part
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/productCategory"
android:hint="#string/ed_product_category"
...
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
<AutoCompleteTextView
android:id="#+id/productCategoryAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
Setting list items to autocompletefield. This code is executed inside onCreate method of EditActivity.
productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(
this,
R.array.product_categories,
R.layout.list_item
);
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);
How I'm trying to set initial value 1 (work but not as expected)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
How I'm trying to set initial value 2 (looks like doesn't work)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setSelection(itemPosition);
Field in creation form
Field in updating form
What I want in updating form
setText() method has second optional boolean parameter - filter. This param is used to filter items from array adapter.
This worked for me in one of the kotlin projects I was working on.
I firstly set text of the AutoCompleteTextView. Then set an adapter to the AutoCompleteTextView.
So the code should look like this;
productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this,R.array.product_categories,R.layout.list_item);
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);
How can I create an empty spinner with no elements in it and add them later?
ArrayAdapter<String> aSpin = ArrayAdapter.createFromResource(this, null , null);
Doesnt work.
I'm curious, in which scenario would you want an empty spinner?
anyway, a spinner with a null object will give you an annoying exception. If you really want a spinner "empty", i would recommend you to create a normal spinner with a fake object. That object would be a string like " ". So, it won't be empty but it will look like if it is.
-- Edit --
In order to use a spinner with objects, you will need an adapter. So, first, if you want an empty spinner, just find its view from your layout and that's all. You will have an empty spinner:
mSpinner = (Spinner)findViewById(R.mLayout.mSpinnerID);
Then, when you need to add your items to the spinner, create an array with those items, then add them to the adapter, and add the adapter to your spinner. For example, let's say that you will have strings:
ArrayList<String> mList = new ArrayList<String>();
mList.add("item 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mList);
mList.setAdapter(adapter);
An Adapter is something different than a Spinner. If you want to create an empty Spinner, just define one in your layout file or create one programmatically. It will not contain any items if you don't set an Adapter on the Spinner (or specify static entries in the xml declaration).
You might want to take a look at the Spinner tutorial.
Hi I have a spinner for which I would like to change its entry. I have created an array in the values folder. I know that I can edit the entry of the spinner by right clicking on it. But I want to know, how can I change the entry of the spinner using code. I was hoping there would be something like spinner5.editEntries
Can someone help please?
Spinner Spinnermiles = (Spinner) findViewById(R.id.Spinnermiles);
String [] arrmile ={"5","10","20","30","40","50","70","80","90","100"};
adapter = new ArrayAdapter<String>(Searching.this,android.R.layout.simple_spinner_item,arrmile);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinnermiles.setAdapter(adapter);
If you want to change item in spinner at position 3 (which is "30" in example),
Set value at that position e.g.
arrmile[3] = "enter new value you want";
and after that call
adapter.notifyDataSetChanged();
then value at that position will be get updated.
I want to create a AutoCompleteTextView in android. The problem is that I want to show whole list of data when user selects AutoCompleteTextView and start filtering data as user types the letters.Please help me in doing this.
Well here is an way how you can do that,
declare an String array -
String[] array = new String[]{"first","second","third","fourth"};
Now, initialize the Adapter with the source.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,array);
Finally fetch the AutoCompleteTextView id from your xml and set the adapter.
AutoCompleteTextView mView = (AutoCompleteTextView)
findViewById(R.id.myAutoTextView);
mView.setAdapter(adapter);
I created a list view which extends ListActivity and I have a search field at the top of the page
but I don't know how to write it , I want a search like search contact (type just only 1 char and listview will change, don't have to press any button)
please give me some example code
thanks
I think you should see this
If you are currently using a CursorAdapter to display the List, than you can create a custom CursorAdapter which does filtering automatically as you type keys. The following webpage provides an excellent example of this: http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/
Good luck!
Create a AutoCompleteTextView in your Layout,then put this in your class file,
AutoCompleteTextView txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_Val);
txtPhoneNo.setAdapter(adapter);
where name_Val is the String Array that contains "DATA"